在我的应用程序中,我保存了一个图像文件(从相机手机拍摄的照片),但有时如果我想在Imageview中立即显示其缩略图,则会出现错误。为什么?保存过程太慢了吗?我可以使用替代方法来保存我的图像文件吗?
我在这里打电话给我的相机:
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.v("Crea file Immagine", "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.v("photofile",String.valueOf(photoFile));
Uri mSelectedImageUri= Uri.fromFile(photoFile);
Log.v("mSelectedImageUri", String.valueOf(mSelectedImageUri));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mSelectedImageUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
}
}
});
这里我保存了我的图片文件:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
sharedPreference.save(context,"nomeFile",timeStamp);
File storageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM) + File.separator + "BottiglieDiVino");
boolean success;
if (!storageDir.exists()) {
success = storageDir.mkdir();
if (success) {
// Do something on success
Log.v("Crea /BottiglieDiVino", "Ho Creato nuova directory");
} else {
// Do something else on failure
Log.v("Crea /BottiglieDiVino", "ERRORE nel creare nuova directory");
}
}
File image = new File(storageDir, timeStamp + ".jpg");
// Save a file: path for use with ACTION_VIEW intents
percorso = image.getAbsolutePath();
sharedPreference.save(context,"percorso", percorso);
Log.v("percorso", percorso);
return image;
}
在这里我得到了这个错误
java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
libcore.io.IoBridge.open(IoBridge.java:409)
java.io.FileInputStream.<init>(FileInputStream.java:78)
java.io.FileInputStream.<init>(FileInputStream.java:105)
android.content.ContentResolver.openInputStream(ContentResolver.java:630)
com.example.android.swipetabs.FragmentTab2.decodeSampledBitmapFromUri(FragmentTab2.java:
P.S。在最后一行:)是错误。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
percorso = sharedPreference.getValue(context, "percorso");
sharedPreference.save(context,"check23", "ok");
Log.v("percorso", percorso);
foto.setImageBitmap(display(percorso));
}
}
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) {
try {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options);
请帮帮我。
答案 0 :(得分:0)
注意:为marashmallow或更高版本提供这些权限运行时间,并在您的其他设备授予权限时将代码放入其中正常工作
在mainfest中
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
运行时
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 10;
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
permissions= new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (checkPermissions())
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(demo.this.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.v("Crea file Immagine", "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.v("photofile",String.valueOf(photoFile));
Uri mSelectedImageUri= Uri.fromFile(photoFile);
Log.v("mSelectedImageUri", String.valueOf(mSelectedImageUri));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mSelectedImageUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
}
}
}
});
private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p:permissions) {
result = ContextCompat.checkSelfPermission(this,p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}