我正在申请我的相机功能。我正在捕捉图像并将其传递给另一个活动。我面临的问题是,当我在另一个活动中显示图像时,由于某种原因,它会丢失其原始结果(变得像素化)。这就是我的做法:
private void takePhotoFromCamera() {
if(ActivityCompat.checkSelfPermission(EnterDataView.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}else {
String[] permissionRequest = {Manifest.permission.CAMERA};
requestPermissions(permissionRequest, 8675309);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
if(requestCode == CAMERA_REQUEST){
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
Intent passPhoto = new Intent(this, Photo.class);
passPhoto.putExtra("byteArray",mphoto);
passPhoto.putExtra("Caller", getIntent().getComponent().getClassName());
startActivity(passPhoto);
}
}
}
在其他活动中获取图像:
if(getIntent().hasExtra("byteArray")) {
//ImageView _imv= new ImageView(this);
/*Bitmap _bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);*/
Intent intent_camera = getIntent();
Bitmap camera_img_bitmap = (Bitmap) intent_camera
.getParcelableExtra("byteArray");
//_imv.setImageBitmap(_bitmap);
View view = mInflater.inflate(R.layout.gallery_item, mGallery, false);
ImageView img = (ImageView) view
.findViewById(R.id.id_index_gallery_item_image);
//String uri = getPhotos.getString(getPhotos.getColumnIndex(("uri")));
//Uri mUri = Uri.parse(uri);
//img.setImageURI(mUri);
//byte[] blob = getPhotos.getBlob(getPhotos.getColumnIndex("image"));
//Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
//bmpImage.add(bmp);
img.setImageBitmap(camera_img_bitmap);
mGallery.addView(view);
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/id_index_gallery_item_image"
android:layout_width="@dimen/_300sdp"
android:layout_height="@dimen/_300sdp"
android:layout_alignParentTop="true"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginTop="@dimen/_50sdp"
android:scaleType="centerCrop" />
</RelativeLayout>
我做错了什么?
答案 0 :(得分:6)
您使用的方法不适用于棉花糖上方的所有设备。 按照这个,
在manifest.xml中添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="yourpackagename.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path"/>
</provider>
在资源的xml文件夹中创建provider_path。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="/storage/emulated/0" path="."/>
</paths>
然后在您的活动中添加此项, 声明一个全局变量
private Uri mUri;
private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackagename.fileprovider";
private void takePicture() {
File file = null;
try {
file = createImageFile();
mUri = FileProvider.getUriForFile(context,
CAPTURE_IMAGE_FILE_PROVIDER, file);
Log.d("uri", mUri.toString());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(cameraIntent, REQUEST_CAMERA_STORAGE);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CAMERA_STORAGE) {
if (resultCode == RESULT_OK) {
if (mUri != null) {
String profileImageFilepath = mUri.getPath().replace("//", "/");
sendImage(profileImageFilepath);
}
} else {
Toast.makeText(context, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
}
这是createImageFile()
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "APPNAME-" + timeStamp + ".png";
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
"FOLDERNAME");
File storageDir = new File(mediaStorageDir + "/Images");
if (!storageDir.exists()) {
storageDir.mkdirs();
}
File image = new File(storageDir, imageFileName);
return image;
}
最后,将profileImageFilepath传递给我们要显示的下一个活动
答案 1 :(得分:0)
在这一行中我得到了错误:
private static final String CAPTURE_IMAGE_FILE_PROVIDER =
"com.yourpackagename.fileprovider";
此行是正确的:
private static final String CAPTURE_IMAGE_FILE_PROVIDER =
"com.yourpackagename.provider";
代替fileprovider编写提供程序或使用它
mUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", file);
我的问题解决了。非常感谢你。 很好。