[英]Image taken by camera does not want to display in ImageView
我有一個ImageView,我希望能夠從中加載圖像:
畫廊-很好
相機-不加載任何東西
使用這兩種不同操作時的日志非常相似,除了攝像機操作顯示如下:
我感覺這是為什么未顯示圖像的解釋。 該應用程序不會進行段錯誤或任何操作,從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());
}
}
路徑正確,位圖不為空,一切看起來都還可以,但未顯示圖像。 感謝您的幫助!
我認為錯誤在於文件創建
下面的代碼為我工作
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
}
}
嘗試這個 : -
Handler handler = new Handler();
handler.posDelayed(new Runnable){
@Override
public void run() {
//Your ImageView Code & setImageBitmap
}
}, 20);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.