私は簡単なノートブックアプリケーションを作っています。ノートブックには画像が添付されています。私は写真をうまく撮ることができ、写真が撮影された後、メモ内の画像ビューに正しく表示されます。メモが保存されると、写真へのパスがデータベースに保存されます。パスは正しく保存されます。次に、ユーザーがメモを開くと、データベース内のパスが正しく見つかる - しかし、私はイメージビューに読み込むことができません。むしろ、イメージビューは白くなります。画像を保存するフォルダを手作業で調べると、画像が電話やSDカードに保存されていないことがわかります。保存方法はわかりません。私はインターネットとStackOverflowの全面を見てきましたが、解決策を見いだせませんでした。Android - ファイルパスから画像を表示できません/画像をデバイスに保存できません
public void onIcTakePhotoClick(View icon) {
File imageFile = null;
Intent takePictureIntent;
try {
imageFile = ImageHelper.createImageFile(this);
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
mImagePath = imageFile.getAbsolutePath();
//mImagePath = ImageHelper.getImagePath(imageFile);
takePictureIntent = ImageHelper.dispatchTakePictureIntent(this, imageFile);
startActivityForResult(takePictureIntent, ImageHelper.REQUEST_IMAGE_CAPTURE);
} else {
// error message
}
}
上記の全ての(パス、ファイルを作成し、その意図は)うまく動作:
public static File createImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
public static Intent dispatchTakePictureIntent(Context context, File photoFile) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
// Continue only if the File was successfully created
Uri photoURI = FileProvider.getUriForFile(context,
"com.bergdahl.notebook.fileprovider",
photoFile);
//takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
return takePictureIntent;
}
return null;
}
ファイルパスはこのように見える終わる:「/ストレージ//0 /アンドロイドをエミュレート/data/com.myname.notebook/files/Pictures/JPEG_20160824_213643_135920553.jpg "次は結果を得る:
public static void handleCameraPhoto(Activity context, ImageView imageView, String imagePath) {
if (imagePath != null) {
ImageHelper.setPic(imagePath, imageView);
ImageHelper.galleryAddPic(context, imagePath);
}
}
public static void setPic(String imagePath, ImageView imageView) {
/* There isn't enough memory to open up more than a couple camera photos */
/* So pre-scale the target bitmap into which the file is decoded */
/* Get the size of the ImageView */
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
/* Associate the Bitmap to the ImageView */
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
}
public static void galleryAddPic(Activity context, String imagePath) {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
そして最後に、ノート後で開放のためのコード:
if (mNote.getFilePath() != null) {
mImagePath = mNote.getFilePath();
//ImageHelper.handleCameraPhoto(this, imageView, mNote.getFilePath());
//File file = new File(mImagePath);
//imageView.setImageDrawable(Drawable.createFromPath(mImagePath));
//Uri uri = Uri.parse("file:" + mNote.getFilePath());
//imageView.setImageURI(uri);
}
私はAndroidのチーム(
https://developer.android.com/training/camera/photobasics.html)によって作成されたメソッドを使用している
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ImageHelper.REQUEST_IMAGE_CAPTURE: {
if (resultCode == RESULT_OK) {
ImageHelper.handleCameraPhoto(this, imageView, mImagePath);
}
break;
}
}
}
マニフェストでは、外部ストレージへの書き込み権限を指定しました。繰り返しますが、デバイスに取り込んだイメージを保存する方法を理解する助けが必要です。次回にユーザーがメモを開くときにイメージビューに読み込むことができます。ヘルプのために事前にありがとう!
編集:私はimage.createNewFile()メソッドを試しましたが、結果は悲しいことに同じです。論理テストは真です。
public static File createImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
/*
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
*/
File image = new File(storageDir, imageFileName + ".jpg");
if (!image.exists()) {
try {
boolean test = image.createNewFile();
Log.d("createImageFile", test + "");
} catch (IOException e) {
e.printStackTrace();
}
}
return image;
}
EDIT again:イメージが正しくエミュレータに表示されるようになりましたが、実際のデバイスでは機能しません。エミュレータ6.0、私のデバイス6.0.1を実行します。私のデバイスは許可を与えており、外部ストレージを持っています。私はディレクトリを変更しようとしていましたが、成功しませんでした。
のAndroidデバイスのバージョンを使用を追加しますか? –
一時ファイルの代わりに通常のファイルを作成します。 –
Android 6.0.1を実行している端末でテストしています。私はファイル作成には全く新しいので、通常のファイルを作成する方法はわかりません。どのように私が1つを作成するかの例を私に提供できますか? – LGW