我有一个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());
}
}
路径正确,Bitmap不为空,一切看起来都还可以,但不显示图像。谢谢你的帮助!
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
试试这个: -
Handler handler = new Handler();
handler.posDelayed(new Runnable){
@Override
public void run() {
//Your ImageView Code & setImageBitmap
}
}, 20);