gpt4 book ai didi

Android - ImageView 不会显示用相机拍摄的照片

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:10 26 4
gpt4 key购买 nike

请多多包涵...我 DAYS 一直在四处寻找可以启动相机 Activity 、拍照并将其放在simple ImageView >.< 下面发布的代码启动了 Activity 并正常拍摄了照片,但是 图像没有显示在 ImageView 上! 缺少什么? :'(

public class MainActivity extends Activity
{
private static final int PICK_IMAGE = 0;

private static final int PICK_IMAGE_FROM_GALLERY = 1;

private Button mBtnCamera, mBtnGallery, mBtnCancel;

private ImageView mImageView;

private Uri mURI;
private String mPhotoPath;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mImageView = (ImageView) findViewById(R.id.imgDisplayImage);

mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera);
mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery);
mBtnCancel = (Button) findViewById(R.id.btnCancel);

mBtnCamera.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
Intent camera = new Intent();

camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

camera.putExtra("crop", "true");

File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg"));

camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI);

startActivityForResult(camera, PICK_IMAGE);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE)
{
// Result includes a Bitmap thumbnail?
if (data != null)
{
if (data.hasExtra("data"))
{
//Bitmap thumbnail = data.getParcelableExtra("data");

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

mImageView.setImageBitmap(thumbnail);
}
}
// If there is no thumbnail data, the image will have been stored in target output URI.
else
{

Cursor cursor = getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, new String[]
{
Media.DATA,
Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION
},
Media.DATE_ADDED,
null,
"date_added ASC"
);

if (cursor != null && cursor.moveToFirst())
{
do
{
mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
mPhotoPath = mURI.toString();
}
while (cursor.moveToNext());
cursor.close();
}

// Resize full image to fit out in image view.
int width = mImageView.getWidth();
int height = mImageView.getHeight();

BitmapFactory.Options factoryOptions = new BitmapFactory.Options();

factoryOptions.inJustDecodeBounds = true;

BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);

int imageWidth = factoryOptions.outWidth;
int imageHeight = factoryOptions.outHeight;

// Determine how much to scale down the image
int scaleFactor = Math.min(
imageWidth/width,
imageHeight/height
);

// Decode the image file into a Bitmap sized to fill view

factoryOptions.inJustDecodeBounds = false;
factoryOptions.inSampleSize = scaleFactor;
factoryOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);

mImageView.setImageBitmap(bitmap);
}
}
}
}

最佳答案

I had same problem in some devices of samsung android Then I implemented logic to get path of captured photo.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
do {
uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
photoPath = uri.toString();
}while(cursor.moveToNext());
cursor.close();
}

if(photoPath != null) {
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
///Do Implement your logic whatever you want.
mImageView.setImageBitmap(bitmap);
}
}
}

关于Android - ImageView 不会显示用相机拍摄的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15139456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com