我有一個自定義相機Android應用程序,根據Google教程將捕獲的圖像保存到外部存儲器中,然後Media Scanner觸發Gallery來檢測它們。在內部存儲器中存儲圖像的位置?
但是我對此沒有SD卡插槽,而不是外部存儲器LG G2新的客戶端 - 僅限內部。
我向他解釋,我只能讓我的應用程序存儲圖像的應用程序的內部緩存,並且他會通過某種根瀏覽器訪問它們。
但他聲稱,其他購置相機的應用程序存儲他們的圖像,以便Gallery可以檢測到它們。怎麼樣?請幫助 - 我需要讓我的應用能夠做到這一點。
謝謝!
UPD:以下是我的代碼,爲其他設備和主要處理外部存儲器的工作原理:
public static File getBaseFolder() {
File f;
f = null;
try {
f = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
} catch (Exception e) {
Utils.toasterLong("Error accessing storage: " + e.getMessage());
}
return f;
}
public static File getImageFolder() {
File f;
f = null;
try {
f = new File(getBaseFolder(), "Magic");
} catch (Exception e) {
Utils.toasterLong("Error accessing storage: " + e.getMessage());
}
return f;
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = getImageFolder();
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
和代碼的一部分圖像寫入到外部存儲器,並觸發掃描儀:
{
pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
writeErrorDirty = true;
if (MyDebug.LOG)
Log.d(TAG,
"Error creating media file, check storage permissions");
} else if (MyDebug.LOG)
Log.d(TAG, "Valid path=" + pictureFile.getAbsolutePath());
FileOutputStream out = null;
try {
out = new FileOutputStream(pictureFile);
picTaken_mod.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
e.printStackTrace();
writeErrorDirty = true;
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
writeErrorDirty = true;
if (MyDebug.LOG)
Log.d(TAG,
"Error writing media file, check storage permissions");
}
} // end try
try {
MediaScannerConnection.scanFile(grandContext,
new String[] { pictureFile.getAbsolutePath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
}
});
} catch (Exception e) {
if (MyDebug.LOG)
Log.d(TAG,
"Error broadcasting the image: " + e.getMessage());
// writeErrorDirty = true;
}
}
當你描述: - 他們寫的圖像外部存儲,然後使用MediaScannerConnection通知畫廊和使用MediaStore的ContentProvider,該文件有其他應用程序。 - 你描述了我的應用。它適用於數百種設備,但不適用於此G2 G2 – rommex 2014-12-07 16:26:26
@rommex:然後,您要麼不在G2上寫入外部存儲器,要麼G2存在某種錯誤。您可能會考慮編輯您的問題,以發佈您用於編寫該文件的代碼,並通過'MediaStore'索引它。 – CommonsWare 2014-12-07 16:31:29
請在上面編輯的帖子中查看我的代碼,謝謝! – rommex 2014-12-07 18:03:54