【问题标题】:Image not setting in ImageView after taking picture from Camera从相机拍照后图像未在 ImageView 中设置
【发布时间】:2014-01-22 03:47:06
【问题描述】:

我已经开发了自己的相机应用程序进行强制配置,当我尝试在下一个显示是否保存的活动中显示捕获的图像时?

我无法获取我捕获并显示在我的 ImageView 上的图像。我绝对会通过正确的路径获得absPathUri

代码 sn-ps:-

imgView = (ImageView)findViewById(R.id.picView);
Bundle b= getIntent().getExtras();
absPathUri = Uri.parse(b.getString("URI"));
Toast.makeText(getApplicationContext(), ""+absPathUri, Toast.LENGTH_SHORT).show();
if(absPathUri!=null)
{
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
    imgView.setImageURI(absPathUri);
}

关于我无法设置 ImageView 的原因进一步深入,抛出 Null Pointer Exception,这是由于 File Not Found。如果应用程序处于调试模式,它会在 ImageView 上显示正确的图像。似乎 mediaStore 会刷新,直到调试成功。

File imgFile = new  File(getPath(absPathUri));
Toast.makeText(getApplicationContext(), "ImageFile Exists"+imgFile.exists(), Toast.LENGTH_SHORT).show();
if(imgFile.exists())
 {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imgView.setImageBitmap(myBitmap);
}

  public String getPath(Uri photoUri) {

    String filePath = "";
    if (photoUri != null) {
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        try
        {

            Cursor cursor = getContentResolver().query(photoUri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            cursor.close();
        }catch(Exception e)
        {
        Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();   
        }
    }
    return filePath;
}

尝试过的解决方案:-

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));

谁能指导我在 ImageView 上显示图像时哪里出错了?

【问题讨论】:

标签: android uri android-camera android-imageview


【解决方案1】:

正如 gnuanu 所建议的,setImageURI() 不适合用作 UI 线程上的读取和解码,这可能会导致延迟打嗝。

最好使用以下内容:-

setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

这些方法仍然没有解决我的问题。当我用相机拍照并点击它发送到下一个活动时,这可能会导致延迟打嗝。

所以最好在一段时间后转到另一个活动。 .只需一秒钟就可以完全通过它。

解决我的问题的片段:-

final Intent picIntent = new Intent(CameraActivity.this,PicSaveActivity.class);
picIntent.putExtra("URI", pathUri.toString());
Handler handler = new Handler() 
{
public void handleMessage(Message msg) {
    startActivityForResult(picIntent, PICTAKEN);
    };
};
 Message msg = handler.obtainMessage();
 handler.sendMessageDelayed(msg, 1000);

在 Next Activity 中,捕获 URI 并像在横向模式下一样旋转图像。

 if(absPathUri!=null)
{
    Bitmap myImg = BitmapFactory.decodeFile(absPathUri.getPath());
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
            matrix, true);
    imgView.setImageBitmap(rotated);
    }

【讨论】:

    【解决方案2】:

    我真的不知道你的代码有什么问题,我的回答使用了一种完全不同的方法来用相机拍照并在 ImageView 中显示它,但它是一个可行的解决方案,可能会对你有所帮助。

    在您的第一个 Activity(称为 CameraActivity)中,您可以执行以下操作:

    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    
    public class CameraActivity extends PortraitActivity {
      private static final int CAMERA_REQUEST = 1888;
      private ImageView imageView;
      private Bitmap photo;
    
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
      }
    
      /** Method to open camera and take photo */
      public void takePhoto(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
      }
    
      /** Method called after taking photo and coming back to current Activity */
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {
          photo = (Bitmap) data.getExtras().get("data");
          Intent intent = new Intent(this, NewActivity.class);
          intent.putExtra("BitmapImage", photo);
          startActivity(intent);
        }
      }
    }
    

    在第二个活动(称为NewActivity)中,只需将以下代码放入onCreate方法中,然后将imageView分配给XML布局中的相应视图。

          Intent intent = getIntent();
          Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
          imageView.setImageBitmap(photo);
    

    【讨论】:

    • 因为我需要 Focus ON 每次从我的应用程序中拍摄照片,因为您展示的解决方案不起作用。
    【解决方案3】:

    reference 明确指出:文件的路径包含在Intent.mData 字段中。

    因此,你需要

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, absPathUri));
    

    您可能还需要在获得结果之前让广播完成;使用

    imgView.post(new Runnable() { public void run() { imgView.setImageURI(absPathUri); } }
    

    应该够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多