Activity returns image

I want to return the bitmap to my activity, so other applications can use it.

The return text is clear.

Intent data = new Intent(); data.putExtra("text1", "text."); data.putExtra("text2", "longer text."); setResult(RESULT_OK, data); 

But how to return a bitmap?

Additional information: Activity has several intentions for everyone who wants to get an image.

 <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.OPENABLE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> <data android:mimeType="vnd.android.cursor.dir/image" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PICK" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> <data android:mimeType="vnd.android.cursor.dir/image" /> </intent-filter> 

EDIT: Here is a solution in one function:

 public void finish(Bitmap bitmap) { try { File folder = new File(Environment.getExternalStorageDirectory() + "/Icon Select/"); if(!folder.exists()) { folder.mkdirs(); } File nomediaFile = new File(folder, ".nomedia"); if(!nomediaFile.exists()) { nomediaFile.createNewFile(); } FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Icon Select/latest.png"); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); File bitmapFile = new File(Environment.getExternalStorageDirectory() + "/Icon Select/latest.png"); if(bitmapFile.exists()) { Intent localIntent = new Intent().setData(Uri.fromFile(bitmapFile)); setResult(RESULT_OK, localIntent); } else { setResult(RESULT_CANCELED); } super.finish(); } catch (Exception e) { e.printStackTrace(); Log.d("beewhale", "Error writing data"); } } 
+7
source share
2 answers

While I agree with Ovidiu Latcu , you may run into memory problems.

It might be better to save the bitmap in a temporary location on the SD card. And then access it from there. (Writing and reading a bitmap image to a file).

Alternatively, if you want to go with putting an array of bytes as optional, first copy the bitmap to a different format, for example. Jpeg or similar, this will again reduce memory problems.

A simple 8M image is 32 MB (4 layers). And this exceeds the allowed memory usage on most (if not all) phones for the application.

Storing directly in the storage is how the built-in camera application works to avoid this problem.

Here is my code for this:

 public void showCameraScreen(View view) { // BUILT IN CAMERA Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); this.startActivityForResult(camera, 1); } private File getTempFile(Context context) { // it will return /sdcard/MyImage.tmp final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName()); if (!path.exists()) { path.mkdir(); } return new File(path, "MyImage.tmp"); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { final File file = getTempFile(this); byte[] _data = new byte[(int) file.length()]; try { InputStream in = new FileInputStream(file); in.read(_data); in.close(); in = null; //DO WHAT YOU WANT WITH _data. The byte array of your image. } catch (Exception E) { } } } 

Note. You will also need to write code on your requested intent, which is stored in the passed Uri. Here the built-in api camera does this.

+5
source

You can use putExtra(key,byte[]) and pass the bytes your Bitmap , and then use BitmapFactory.decodeByteArray() in your onActivityResult to create a Bitmap .

EDIT: You can also return Uri your resource / image.

0
source

All Articles