1

画像をロードできるようにしたいImageViewがあります。

  1. ギャラリー-問題なく動作します

  2. カメラ-何もロードしません

これらの2つの異なるアクションを使用している間のログは、カメラのアクションが最後まで表示されることを除けば、非常によく似ています。

  • 10-21 23:24:18.304:D / android.widget.GridLayout(4447):水平方向の制約:x6-x0> 720、x5-x0> 720、x5-x4 <20、x4-x3 <142、x3-x2 <70、x2-x1 <32、x1-x0<138は一貫性がありません。完全に削除:x5-x4<20。
  • 10-21 23:24:18.324:D / android.widget.GridLayout(4447):垂直方向の制約:y1-y0> 468、y2-y1> 30、y3-y2> 120、y3-y0> 1140、y4-y3 > 612、y4-y0 <1140、y3-y2 <120、y2-y1<30は一貫性がありません。完全に削除:y4-y0 <1140、y3-y2<120。

これが画像が表示されない理由の説明だと思います。アプリはセグメンテーション違反などを発生させません。UIの観点からは何も起こりません。

コード(カメラアクションとギャラリーアクションの両方で共有)は次のとおりです。

protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == Activity.RESULT_OK) {
    Uri uri = data.getData();

    if ((requestCode == SELECT_PICTURE) || (requestCode == PICTURE_RESULT)) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getRealPathFromURI(selectedImageUri);
        mImageView = (ImageView) findViewById(R.id.imageView1);
        int x = mImageView.getWidth();
        int y = mImageView.getHeight();
        if (x == 0 || y == 0) {
            Display d = getWindowManager().getDefaultDisplay();
            x = d.getWidth();
            y = d.getHeight();
        }
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options(); 
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(selectedImagePath, opts);
            // Calculate inSampleSize
            opts.inSampleSize = calculateInSampleSize(opts, x, y);                  
            // Decode bitmap with inSampleSize set
            opts.inJustDecodeBounds = false;
            mPicture = BitmapFactory.decodeFile(selectedImagePath, opts);
            mPicture.recycle(); //otherwise multiple calls segfault
            // create a matrix object
            Matrix matrix = new Matrix();
            matrix.postRotate(90); // clockwise by 90 degrees
            // create a new bitmap from the original using the matrix to transform the result
            Bitmap rotatedBitmap = Bitmap.createBitmap(mPicture, 0, 0, mPicture.getWidth(), mPicture.getHeight(), matrix, true);

            //set image view
            mImageView.setImageBitmap(rotatedBitmap);
        } catch (Exception e) {                 
            System.out.println("Bitmap could not be decoded." + e.getMessage());
        }
    }

パスは正しく、ビットマップはnullではなく、すべてが正常に見えますが、画像は表示されません。助けてくれてありがとう!

4

2 に答える 2

1

エラーはファイルの作成にあると思います

この以下のコードは私のために働いた

File photofile = new File(Environment
                    .getExternalStorageDirectory(),"sample"
                    + System.currentTimeMillis() + ".jpg");
            photFileUri = Uri.fromFile(photofile);
            photoPath = photofile.getAbsolutePath();
            Log.v("camera photo path is    ","          "+photoPath);
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photFileUri);
            cameraIntent.putExtra("return-data", true);
            startActivityForResult(cameraIntent,1);

OnActivityForResult ()メソッドでは、パスを取得します

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
    case 1:
                //here u can use **photoPath** to display image
}

}

于 2012-10-22T09:23:23.323 に答える
0

これを試して : -

 Handler handler = new Handler();
 handler.posDelayed(new Runnable){
  @Override
public void run() {
 //Your ImageView Code & setImageBitmap
}

  }, 20);
于 2012-10-22T07:05:56.647 に答える