gpt4 book ai didi

android - 从图库中选取的图像未在 ImageView 中设置

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:55 25 4
gpt4 key购买 nike

我正在制作一个按钮和 ImageView ,如果我单击该按钮,我想显示 3 个选项,从图库中选择图片,拍照并取消。选择图像后,它应该替换 ImageView ,但是当我从图库中选取图像时,它不会替换 ImageView ,当我选择拍照选项时,我的应用程序停止并给出错误:

{FileUriExposedException: file:///storage/emulated/0/temp.jpg exposed beyond app through ClipData.Item.getUri()"}

我附加了我在按钮 onClickListener 中调用的函数。

// function to choose profile image
private void selectImage() {

final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

AlertDialog.Builder builder = new AlertDialog.Builder(student_form.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);

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

@SuppressLint("LongLogTag")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);

imageview.setImageBitmap(bitmap);

String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {

Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image from gallery......******************.........", picturePath+"");
imageview.setImageBitmap(thumbnail);
}
}
}

最佳答案

我找到了问题的解决方案,现在我可以通过相机和画廊将图像设置为 ImageView ,我需要做的是为api23+添加实时权限,更改上面selectImage()的代码代码。

private void selectImage() {

final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

AlertDialog.Builder builder = new AlertDialog.Builder(student_form.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{

Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);

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

@SuppressLint("LongLogTag")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(bitmap);

}

} else if (requestCode == 2) {



String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

if (EasyPermissions.hasPermissions(this, galleryPermissions)) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();

int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();

Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
imageview.setImageBitmap(thumbnail);
} else {
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
}


}
}

在最后一个花括号的正上方添加以下代码,这是为了检查相机的实时权限

 public void EnableRuntimePermission(){
final int RequestPermissionCode = 1 ;

if (ActivityCompat.shouldShowRequestPermissionRationale(student_form.this,
Manifest.permission.CAMERA))
{

Toast.makeText(student_form.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();

} else {

ActivityCompat.requestPermissions(student_form.this,new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);

}
}

@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

final int RequestPermissionCode = 1 ;
switch (RC) {

case RequestPermissionCode:

if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {

Toast.makeText(student_form.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();

} else {

Toast.makeText(student_form.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();

}
break;
}
}

并且不要忘记调用方法 EnableRuntimePermission();在初始化变量时开始。

关于android - 从图库中选取的图像未在 ImageView 中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49468714/

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