2017-03-21 58 views
0

在我的代碼中,我在位圖上獲得了空值,但Image創建成功,而且uri很好,但是BitmapFactory.decodeFile()返回null。如何在Android中從片段中獲取圖像

我用下面的代碼片斷:

public class CameraImage extends Fragment { 

private static final int TAKE_PHOTO_CODE = 1888; 
Button button; 
private Uri fileUri1,fileUri; 
ImageView imageView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    final View rootView = inflater.inflate(R.layout.camera_image, 
      container, false); 

    button = (Button) rootView.findViewById(R.id.button); 
    imageView = (ImageView) rootView.findViewById(R.id.imageview); 

    button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    fileUri1 = getOutputMediaFileUri(); 
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); 

     } 
    }); 

    return rootView; 

} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == TAKE_PHOTO_CODE) { 
     if (resultCode == Activity.RESULT_OK) { 

      BitmapFactory.Options options; 


       try { 
        Bitmap bitmap = BitmapFactory.decodeFile(pathname); 
        showDialog(bitmap, 1); 
       } catch (OutOfMemoryError e) { 
        try { 
         options = new BitmapFactory.Options(); 
         options.inSampleSize = 2; 
         Bitmap bitmap = BitmapFactory.decodeFile(pathname, options); 
         imageView.setImageBitmap(bitmap); 
        } catch (Exception e2) { 
         Log.e("Camera", "" + e2 + " " + e); 
        } 
       } 
     } 
    }   
} } 

public Uri getOutputMediaFileUri() { 
    return Uri.fromFile(getOutputMediaFile()); 
} 

private static File getOutputMediaFile() { 

    File mediaStorageDir = new File(
      Environment 
        .getExternalStoragePublicDirectory(MyApplication.getAppContext().getFilesDir().getAbsolutePath()), 
      IMAGE_DIRECTORY_NAME); 

    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
        + IMAGE_DIRECTORY_NAME + " directory"); 
      return null; 
     } 
    } 
    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
      Locale.getDefault()).format(new Date()); 
    File mediaFile; 

    try { 
     mediaFile = File.createTempFile(
       "IMG_" + timeStamp, 
       ".jpg",   
       mediaStorageDir  
     ); 
    } catch (Exception e) { 
     Log.e("Camera", "" + e); 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       , "IMG_" + timeStamp + ".jpg"); 
    } 
    return mediaFile; 
} 

在上面的代碼中我得到onActivityResult位圖空但我得到URI從該圖像是存在於該位置。

回答

2

有兩種類型的圖像,我們可以通過相機獲得:通過使用

1)縮略圖以下代碼在onActivityResult:

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
       Log.i(TAG, "" + photo + "/" + data.getExtras().get("data") + "/" + data.getData()); 
       showDialog(photo, 1); 

2)通過在onActivityResult使用下面的代碼全程質量IMMAGE:

BitmapFactory.Options options; 
       options = new BitmapFactory.Options(); 
       options.inSampleSize = 2; 
       Bitmap bitmap = BitmapFactory.decodeFile(pathname, options); 
       showDialog(bitmap, 1); 

3)使用第三方安逸:

編譯 'com.mindorks:paracamera:0.2.2'

paracamera github

0

我解決我的問題:

通過改變cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri1); 
+0

只是我的錯誤 –

+0

你有另一個名爲'fileUri'的變量嗎?它不在您在此處發佈的代碼中。 –

+0

@ Code-Apprentice現在檢查它 –

相關問題