gpt4 book ai didi

android - 相机和图库 Intent 获取图像并复制到android中的另一个文件夹中

转载 作者:搜寻专家 更新时间:2023-11-01 08:35:19 24 4
gpt4 key购买 nike

我正在尝试从相机、画廊和谷歌照片上传照片。没有相机代码库和谷歌照片工作正常但是当我添加相机代码时 Intent 没有返回结果为 -1 和画廊和谷歌照片也没有正常工作::它正在将图像复制到另一个文件夹但需要退出应用程序以在 ImageView 中再次显示图像。这是我的代码

final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Picture");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getImageUri();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// Intent cameraIntent = getCameraIntent();
startActivityForResult(cameraIntent, 2);

}
else if (options[item].equals("Choose from Gallery"))
{
if (Build.VERSION.SDK_INT <= 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
} else if (Build.VERSION.SDK_INT > 19) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);


}
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();

这是 Activity 结果的代码

 public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String selectedImagePath=null;
if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity().RESULT_OK && null != data) {
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
decodeFile(selectedImagePath);
}else if(requestCode==2&&resultCode==getActivity().RESULT_OK &&data!=null){
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
decodeFile(selectedImagePath);
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Log.d("uri",selectedImagePath+":"+selectedImageUri);
}
}

复制图片的代码

  try {
File sd = Environment.getExternalStorageDirectory();
File directory = Environment.getExternalStorageDirectory();
String destinationPath = Environment.getExternalStorageDirectory().toString() + "/IASFolders/"+Configuration.empcode+".jpg";
if (sd.canWrite()) {
File source= finalFile;
File destination= new File(destinationPath);
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}else {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
}

在 dst.close() 之后它正在退出但没有复制任何东西

最佳答案

试试这个...(已测试)

    private static final int PICK_IMAGE = 1;
private static final int REQUEST_CAMERA = 2;

final String[] items = new String[]{"Camera", "Gallery"};
new AlertDialog.Builder(getActivity()).setTitle("Select Picture")
.setItems(options, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Gallery")) {
if (Build.VERSION.SDK_INT <= 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
} else if (Build.VERSION.SDK_INT > 19) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
}else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
}).show();

Activity 结果代码

     @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
decodeFile(selectedImagePath);
} else if (requestCode == REQUEST_CAMERA && resultCode == getActivity().RESULT_OK && null != data) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
profileImage.setImageBitmap(photo);

// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getActivity().getApplicationContext(), photo);

// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
decodeFile(finalFile.toString());
}
}
public String getRealPathFromURIForGallery(Uri uri) {
if (uri == null) {
return null;
}
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}

关于android - 相机和图库 Intent 获取图像并复制到android中的另一个文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37130344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com