0

我面临从 android 相机拍照然后将其显示给下一个活动的问题。过程正常,但在 10 张图片中,有 1 张图片丢失。

步骤:活动 a 我调用相机并拍照,然后我将其 URI 的活动 b 传递给在图像视图中显示,重复此循环 10 次 1-2 次图片丢失,并且在拍照时观察到 2 秒的延迟和以空白屏幕的形式显示到下一个屏幕的图像视图中。

请检查代码并指导我在这里做错了什么?

活动一

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==CAMERA_IMAGE_CAPTURE && resultCode==Activity.RESULT_OK){

        String[] projection = {
        MediaStore.Images.Thumbnails._ID,  // The columns we want
        MediaStore.Images.Thumbnails.IMAGE_ID,
        MediaStore.Images.Thumbnails.KIND,
        MediaStore.Images.Thumbnails.DATA};
        String selection = MediaStore.Images.Thumbnails.KIND + "="  + // Select only mini's
        MediaStore.Images.Thumbnails.MINI_KIND;

        String sort = MediaStore.Images.Thumbnails._ID + " DESC";

       //At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
       Cursor myCursor = this.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);

       long imageId = 0l;
       long thumbnailImageId = 0l;
       String thumbnailPath = "";

       try{
        myCursor.moveToFirst();
       imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
       thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
       thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
       }
       finally{myCursor.close();}

        //Create new Cursor to obtain the file Path for the large image

        String[] largeFileProjection = {
        MediaStore.Images.ImageColumns._ID,
        MediaStore.Images.ImageColumns.DATA
        };

        String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
        myCursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
       String largeImagePath = "";

       try{
        myCursor.moveToFirst();

       //This will actually give you the file path location of the image.
       largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
       }
       finally{myCursor.close();}
        // These are the two URI's you'll be interested in. They give you a handle to the actual images
        Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
        Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));

        Intent next= new Intent(this,AddListing.class);

        next.putExtra("imageUriThumb", uriThumbnailImage.toString());
        next.putExtra("imageUriFull", uriLargeImage.toString());

        startActivity(next);

   }

   }

活动 B

String uri = bundle.getString("imageUriThumb");
            Uri myThumbUri = Uri.parse(uri);

            try {
                tempBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myThumbUri);
                imgThumbnail.setImageBitmap(tempBitmap);

                ByteArrayOutputStream bao = new ByteArrayOutputStream();
                tempBitmap.compress(Bitmap.CompressFormat.PNG, 90, bao);

                byte [] ba = bao.toByteArray();
                imageBytesString = Base64.encodeToString(ba, 0);

            } catch (Exception e) {

            } 
4

1 回答 1

0

我用以下几点结束了这个问题:

我已经在互联网上搜索了这个问题,实际上这些手机没有使用 intent.get("data") 获取图像,这就是我使用上述技术的原因。我已经尝试过使用三星王牌手机、三星 Galaxy 和同样的问题。我想整个三星都不能完美地工作,当你需要一次又一次地拍照时,会有延迟响应:( HTC 等其他手机都可以正常工作。

于 2012-10-05T19:24:50.077 回答