英文:
How to transfer image to another activity
问题
private void openCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "Taking pic from the Camera");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SelectedImage.setImageURI(image_uri);
try {
Bitmap ImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), image_uri);
detectImage(ImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
英文:
I have two activity, First with button, which calls void openCamera, and second, where I need to get bitmap.<br>
I have two questions: <br>
- Which line of code is saving pictures? <br>
- How I can take a bitmap from OnActivityResult and get it in another activity?
private void openCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "Taking pic from the Camera");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SelectedImage.setImageURI(image_uri);
try {
Bitmap ImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), image_uri);
detectImage(ImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
答案1
得分: 1
不要考虑通过 Bundle
进行传递(文档:https://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject)
我认为,你应该将图像保存在应用的内部存储中,然后在第二个活动中加载它。
以下是解释如何做到这一点的答案:https://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android
英文:
Don't think about passing it through Bundle
(docs: https://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject)
I think, you should save the image in internal storage of your app and then load it in your second activity.
Here are the answers explaining how to do that: https://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android
答案2
得分: 0
将图像URI作为字符串与活动意图一起传递
mIntent.putExtra("image", image_url.toString());
在接收活动中获取URI并将代码移动到生成位图的位置。
String image_url = getIntent().getStringExtra("image");
//获取位图的代码
英文:
Pass image uri as string along with activity intent
mIntent.putExtra("image", image_url.toString());
In the receiving activity get the uri and move your code to generate bitmap there.
String image_url = getIntent().getStringExtra("image");
//your code to get bitmap
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论