媒体扫描仪运行

时间:2012-11-05 12:23:28

标签: android android-camera android-gridview android-mediascanner

在我的应用程序中,用户通过相机功能捕获图像,之后我将在网格视图中更新图像。但是在长时间搜索后我发现需要手动运行媒体扫描程序时不会更新这些图像。我为此编写代码。但是当我反复捕捉图像时,它给出了错误。我发布了我的日志猫错误....

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卡。所以任何人都可以指导我如何运行媒体扫描仪以及我在哪里可以运行我的code.i搜索很多但这些不满足我的需求。

0 个答案:

没有答案