在我的应用程序中,用户通过相机功能捕获图像之后,我将在网格视图中更新图像。但是经过长时间搜索后这些图像没有更新,我发现需要手动运行媒体扫描仪。我为此编写代码。但是当我捕获时图像反复出现错误。下面我发布了我的日志猫错误....
11-05 17:17:17.565: E/AndroidRuntime(30801): FATAL EXCEPTION: main
11-05 17:17:17.565: E/AndroidRuntime(30801): java.lang.IllegalStateException: not connected to MediaScannerService
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.media.MediaScannerConnection.scanFile(MediaScannerConnection.java:164)
11-05 17:17:17.565: E/AndroidRuntime(30801): at com.imagedatabase.Category$6.onMediaScannerConnected(Category.java:512)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.media.MediaScannerConnection.onServiceConnected(MediaScannerConnection.java:249)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ActivityThread.java:1247)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.app.ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run(ActivityThread.java:1264)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.os.Handler.handleCallback(Handler.java:587)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.os.Looper.loop(Looper.java:123)
11-05 17:17:17.565: E/AndroidRuntime(30801): at android.app.ActivityThread.main(ActivityThread.java:4627)
下面我发布了我的代码,我将在其中运行这个代码,并在评论中解释每一步。
enter code here
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
//when camera button is pressed it starts
startCameraActivity();
}
}
public void startCameraActivity() {
//seperating the images with date
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
String newPicFile = df.format(date);
direct = new File(Environment.getExternalStorageDirectory() + path
+ "/" + newPicFile + ".jpg");
outputFileUri = Uri.fromFile(direct);
//here i am starting the camra
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
enter code here
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("image data base", "resultCode: " + resultCode);
switch (resultCode) {
case 0:
//when user cancel the image take here i am deleted the urifile
File file = new File(outputFileUri.getPath());
boolean deleted = file.delete();
Log.i("image data based", "User cancelled");
break;
case -1:
onPhotoTaken();
break;
}
}
//在这张照片上我写了媒体扫描仪的代码
enter code here
protected void onPhotoTaken() {
Log.i("MakeMachine", "onPhotoTaken");
taken = true;
SharedPreferences path_shared = Category.this.getSharedPreferences(
"path", MODE_WORLD_WRITEABLE);
imagePath = path_shared.getString("mypath", "0");
File newFile = new File(imagePath);
final Uri outputFileUri = Uri.fromFile(newFile);
mScanner = new MediaScannerConnection(Category.this,
new MediaScannerConnection.MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
mScanner.scanFile(outputFileUri.getPath(), null /* mimeType */);
}
public void onScanCompleted(String path, Uri uri) {
if (path.equals(outputFileUri.getPath())) {
mScanner.disconnect();
// we need to create new UI thread because, we can't
// update our mail thread from here
// Both the thread will run one by one, see
// documentation of android
Category.this.runOnUiThread(new Runnable() {
public void run() {
}
});
}
}
});
mScanner.connect();
}
我希望你明白我的意思我也试过了
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + \
outputFileUri+ Environment. getExternalStorageDirectory())));
但是我认为它扫描整个 sd 卡有点慢。所以任何人都可以指导我如何运行媒体扫描仪以及我可以在哪里运行我的代码。我搜索了很多,但这些都不能满足我的需求。