Android mediaScannerConnection.scanFile无法刷新图库中的图像 [英] Android mediaScannerConnection.scanFile failing to refresh images in gallery
问题描述
我应该使用MediaScannerConnection.scanFile方法之外的其他方式刷新图库吗?
Should I be using something other than the MediaScannerConnection.scanFile method to refresh the gallery?
保存新的jpg后,我运行媒体扫描仪以刷新图库应用程序,例如因此
After saving a new jpg I run media scanner to refresh the gallery app like so
MediaScannerConnection.scanFile(this,
new String[] { fullname }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d("ExternalStorage", "@@@@ Scanned " + path + ":");
Log.d("ExternalStorage", "@@@@ -> uri=" + uri);
}
});
日志的输出显示以下正确输出
The output of the log shows the following correct output
@@@@扫描的/data/data/com.mypackage/files/skit_106_01.jpg:
@@@@ Scanned /data/data/com.mypackage/files/skit_106_01.jpg:
@@@@ -> uri = content:// media / external / images / media / 95
@@@@ -> uri=content://media/external/images/media/95
图库应用程序显示没有可用的媒体
The gallery app shows no media available
这段代码已经运行了一段时间了。直到我针对4.4.2版创建了一个Android AVD以进行进一步测试时,问题才浮出水面。
This code has been working perfectly for some time now. It was only when I created an Android avd against version 4.4.2 for further testing that the problem has surfaced.
我似乎建议使用该代码进行刷新画廊应用程序是根据Androids文档确定的,所以也许此问题与我保存文件的方式有关,其代码如下。
The code I have seems to be the recommended way of refreshing the gallery app according to Androids documentation so maybe this issue is related to the way I am saving the file, the code for which is as follows.
UPDATE
该代码检查外部存储空间的可用性,并将其写入外部存储空间;如果外部存储空间不可用,则会将该文件写入内部存储空间。
The code checks for external storage availability and will write to external storage and if external storage is not available it will write the file to internal storage.
private void doSave(String fname, boolean doShare) {
fname = "skit_"+mCurrentSkitId +
"_"+mSkitManager.getCurrentFrameCount(
mCurrentSkitId)+1;
Log.d(TAG, "@@@@ doSave fName = " + fname + " Current skit id = " + mCurrentSkitId);
CharSequence text = getResources().getString(R.string.saved_as)
+ " " + fname;
try {
Bitmap b = mMainView.getSaveBitmap();
if (b == null) {
text = getResources().getString(R.string.save_fail_1);
;
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
return;
}
fname = FileUtils.replaceInvalidFileNameChars(fname);
String value = fname;
File folder = FileUtils.getWritableFolder(this);
/*
* String folder =
* Environment.getExternalStorageDirectory().toString() +
* "/Pictures"; try { folder =
* Environment.getExternalStoragePublicDirectory
* (Environment.DIRECTORY_PICTURES).toString(); } catch
* (NoSuchFieldError e) {
*
* }
*/
String ext = ".jpg";
if (mPrefs.getString("format", "JPG").equals("PNG"))
ext = ".png";
String fullname = folder.getAbsolutePath() + File.separator + value
+ ext;
Map<String, String> hm = new HashMap<String, String>();
hm.put("filename", fullname);
FileOutputStream fos;
if (folder == getFilesDir())
fos = openFileOutput(value + ext, Context.MODE_WORLD_WRITEABLE);
else {
File f2 = new File(fullname);
fos = new FileOutputStream(f2);
}
b.compress(CompressFormat.JPEG, 95, fos);
fos.close();
String[] str = new String[1];
str[0] = fullname;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
MediaScannerConnection.scanFile(this,
new String[] { fullname }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d("ExternalStorage", "@@@@ Scanned " + path + ":");
Log.d("ExternalStorage", "@@@@ -> uri=" + uri);
}
});
}
text = text + value + ext + " "
+ getResources().getString(R.string.saved_end);
;
mLastSaveName = value;
setDetailTitle();
mSkitManager.createFrame(mCurrentSkitId, fullname);
} catch (Exception e) {
Map<String, String> hm = new HashMap<String, String>();
hm.put("text", e.toString());
e.printStackTrace();
text = getResources().getString(R.string.save_fail_2)
+ e.toString();
} catch (Error e) {
Map<String, String> hm = new HashMap<String, String>();
hm.put("text", e.toString());
e.printStackTrace();
text = getResources().getString(R.string.save_fail_2)
+ e.toString();
}
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
执行外部存储可用性检查的代码如下:
The code that does the check for external storage availability looks like this
public static File getWritableFolder(Context context) {
File folder = context.getFilesDir();
if (externalStorageAvailable()) {
try {
folder = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!folder.exists() || !folder.canWrite()) {
folder = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
}
if (!folder.exists() || !folder.canWrite()) {
folder = Environment.getExternalStorageDirectory();
}
} catch (Exception e) {
folder = Environment.getExternalStorageDirectory();
} catch (Error e) {
folder = Environment.getExternalStorageDirectory();
}
if (!folder.exists() || !folder.canWrite()) {
folder = context.getFilesDir();
}
}
return folder;
}
private static boolean externalStorageAvailable() {
boolean mExternalStorageAvailable;
boolean mExternalStorageWriteable;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
return mExternalStorageAvailable && mExternalStorageWriteable;
}
如果有人能够从上述任何一个漏洞中发现漏洞,可能有助于解决这个问题,那就太好了。
If anyone is able to pick holes in any of the above that might help to solve this issue then that would be great.
推荐答案
i与 MediaScannerConnection
,所以我改用了 sendBroadcast
方法。我不知道 sendBroadcast
方法是否不标准/不应该使用,但对我有用。
i was having mixed results with MediaScannerConnection
so i used the sendBroadcast
method instead. I do not know if the sendBroadcast
method is not standard/should not be used but it works for me.
public void galleryAddPic(File currentPhotoPath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(currentPhotoPath);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
还与 MediaScannerConnection
: https://stackoverflow.com/a/4825615/1497188
这篇关于Android mediaScannerConnection.scanFile无法刷新图库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!