Insert Bitmap To Mediastore Alongside Championship As Well As Listing Images Championship Inwards Mediastore

Posted on

It’s a next practise of my quondam posts “Save Bitmap to storage“, “List images inward MediaStore.Images.Media” too “Get thumbnails (in MediaStore.Images.Thumbnails) associated amongst MediaStore.Images.Media“, to present how to insert bitmap to MediaStore amongst title, too listing images championship inward MediaStore.


To insert bitmap to MediaStore, nosotros tin call insertImage(ContentResolver cr, String imagePath, String name, String description) or insertImage(ContentResolver cr, Bitmap source, String title, String description) methods of MediaStore.Images.Media. Both of them, nosotros bring to supply championship (or name) too description. In my understanding, they are only about extra information nosotros tin piece of job past times to MediaStore.

In the insert bitmap side, alter the event inward “Save Bitmap to storage” to insert amongst user editable title. For the instant push “Save to MediaStore” only.

MainActivity.java

package com.blogspot.android_er.androidsavebitmap;  import android.content.ContentResolver; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast;  import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException;  populace degree MainActivity extends AppCompatActivity {      ImageView imageView;     Button btnSaveExternalStorageDirectory;     Button btnSaveMediaStore;     Button btnSaveFileAndMediaStore;      EditText editTextTitle;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          editTextTitle = (EditText)findViewById(R.id.title);          imageView = (ImageView)findViewById(R.id.image);         btnSaveExternalStorageDirectory = (Button)findViewById(R.id.saveExternalStorageDirectory);         btnSaveExternalStorageDirectory.setOnClickListener(new View.OnClickListener(){             @Override             populace void onClick(View v) {                 /*                 * Save bitmap to ExternalStorageDirectory                 */                 // teach bitmap from ImageView                 // non ever valid, depends on your drawable                 Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();                  //always relieve every bit                 String fileName = "test.jpg";                  ByteArrayOutputStream bytes = novel ByteArrayOutputStream();                 bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);                  File ExternalStorageDirectory = Environment.getExternalStorageDirectory();                 File file = novel File(ExternalStorageDirectory + File.separator + fileName);                  FileOutputStream fileOutputStream = null;                 drive {                     file.createNewFile();                     fileOutputStream = novel FileOutputStream(file);                     fileOutputStream.write(bytes.toByteArray());                      Toast.makeText(MainActivity.this,                             file.getAbsolutePath(),                             Toast.LENGTH_LONG).show();                 } grab (IOException e) {                     // TODO Auto-generated grab block                     e.printStackTrace();                 } live {                     if(fileOutputStream != null){                         drive {                             fileOutputStream.close();                         } grab (IOException e) {                             // TODO Auto-generated grab block                             e.printStackTrace();                         }                     }                 }             }});          btnSaveMediaStore = (Button)findViewById(R.id.saveMediaStore);         btnSaveMediaStore.setOnClickListener(new View.OnClickListener(){              @Override             populace void onClick(View v) {                 /*                 * Save bitmap to MediaStore                 */                  //get bitmap from ImageVIew                 //not ever valid, depends on your drawable                 Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();                  ContentResolver cr = getContentResolver();                 //Save amongst user editable championship                 String championship = editTextTitle.getText().toString();                 String description = "Bitmap saved past times ";                 String savedURL = MediaStore.Images.Media                         .insertImage(cr, bitmap, title, description);                  Toast.makeText(MainActivity.this,                         savedURL,                         Toast.LENGTH_LONG).show();              }});          btnSaveFileAndMediaStore = (Button)findViewById(R.id.saveExternalStorageDirectoryMediaStore);         btnSaveFileAndMediaStore.setOnClickListener(new View.OnClickListener(){              @Override             populace void onClick(View v) {                 /*                 * Save bitmap to ExternalStorageDirectory                 */                  //get bitmap from ImageVIew                 //not ever valid, depends on your drawable                 Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();                  //always relieve every bit                 String fileName = "test.jpg";                  ByteArrayOutputStream bytes = novel ByteArrayOutputStream();                 bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);                  File ExternalStorageDirectory = Environment.getExternalStorageDirectory();                 File file = novel File(ExternalStorageDirectory + File.separator + fileName);                  FileOutputStream fileOutputStream = null;                 drive {                     file.createNewFile();                     fileOutputStream = novel FileOutputStream(file);                     fileOutputStream.write(bytes.toByteArray());                      ContentResolver cr = getContentResolver();                     String imagePath = file.getAbsolutePath();                     String cite = file.getName();                     String description = "My bitmap created past times ";                     String savedURL = MediaStore.Images.Media                             .insertImage(cr, imagePath, name, description);                      Toast.makeText(MainActivity.this,                             savedURL,                             Toast.LENGTH_LONG).show();                  } grab (IOException e) {                     // TODO Auto-generated grab block                     e.printStackTrace();                 } live {                     if(fileOutputStream != null){                         drive {                             fileOutputStream.close();                         } grab (IOException e) {                             // TODO Auto-generated grab block                             e.printStackTrace();                         }                     }                 }              }});     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidsavebitmap.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <ImageView         android:id="@+id/image"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@mipmap/ic_launcher" />      <EditText         android:id="@+id/title"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <Button         android:id="@+id/saveExternalStorageDirectory"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Save to ExternalStorageDirectory" />      <Button         android:id="@+id/saveMediaStore"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Save to MediaStore" />      <Button         android:id="@+id/saveExternalStorageDirectoryMediaStore"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Save to ExternalStorageDirectory too insert to MediaStore" /> </LinearLayout>  

uses-permission of “android.permission.WRITE_EXTERNAL_STORAGE” is needed inward AndroidManifest.xml.

 to present how to insert bitmap to MediaStore amongst championship Insert bitmap to MediaStore amongst championship too List images championship inward MediaStoreDownload the files .


In the List Images inward MediaStore:

MainActivity.java

package com.blogspot.android_er.androidlistimages;  import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore; import android.support.v4.content.CursorLoader; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast;  populace degree MainActivity extends AppCompatActivity {      terminal Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;;     terminal String thumb_DATA = MediaStore.Images.Thumbnails.DATA;     terminal String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;     terminal Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;      ListView myList;     SimpleCursorAdapter mySimpleCursorAdapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          myList=(ListView)findViewById(R.id.mylist);          String[] from = {MediaStore.MediaColumns.TITLE};         int[] to = {android.R.id.text1};          CursorLoader cursorLoader = novel CursorLoader(                 this,                 sourceUri,                 null,                 null,                 null,                 MediaStore.Images.Media.TITLE);          Cursor cursor = cursorLoader.loadInBackground();          mySimpleCursorAdapter = novel SimpleCursorAdapter(                 this,                 android.R.layout.simple_list_item_1,                 cursor,                 from,                 to,                 CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);         myList.setAdapter(mySimpleCursorAdapter);         myList.setOnItemClickListener(myOnItemClickListener);     }      AdapterView.OnItemClickListener myOnItemClickListener             = novel AdapterView.OnItemClickListener(){         @Override         populace void onItemClick(AdapterView<?> parent, View view, int position, long id) {             Cursor cursor = mySimpleCursorAdapter.getCursor();             cursor.moveToPosition(position);              int int_ID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));             getThumbnail(int_ID);         }     };      person Bitmap getThumbnail(int id){          String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};          CursorLoader thumbCursorLoader = novel CursorLoader(                 this,                 thumbUri,                 thumbColumns,                 thumb_IMAGE_ID + "=" + id,                 null,                 null);          Cursor thumbCursor = thumbCursorLoader.loadInBackground();          Bitmap thumbBitmap = null;         if(thumbCursor.moveToFirst()){             int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);              String thumbPath = thumbCursor.getString(thCulumnIndex);              Toast.makeText(getApplicationContext(),                     thumbPath,                     Toast.LENGTH_LONG).show();              thumbBitmap = BitmapFactory.decodeFile(thumbPath);              //Create a Dialog to display the thumbnail             AlertDialog.Builder thumbDialog = novel AlertDialog.Builder(MainActivity.this);             ImageView thumbView = novel ImageView(MainActivity.this);             thumbView.setImageBitmap(thumbBitmap);             LinearLayout layout = novel LinearLayout(MainActivity.this);             layout.setOrientation(LinearLayout.VERTICAL);             layout.addView(thumbView);             thumbDialog.setView(layout);             thumbDialog.show();          }else{             Toast.makeText(getApplicationContext(),                     "NO Thumbnail!",                     Toast.LENGTH_LONG).show();         }          render thumbBitmap;     } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidlistimages.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <ListView         android:id="@+id/mylist"         android:layout_width="match_parent"         android:layout_height="match_parent"/> </LinearLayout>  

uses-permission of “android.permission.READ_EXTERNAL_STORAGE” is needed inward AndroidManifest.xml.

 to present how to insert bitmap to MediaStore amongst championship Insert bitmap to MediaStore amongst championship too List images championship inward MediaStoreDownload the files .