0

I know it's a very basic question but i am stuck to resolve this problem. I am working on image-sketching mobile app i have done all the work now I just want to store a resulting bitmap-image into internal memory.I have created a method "mageStore()" for image-storing purposes please write code there. I will be very thankful to you.

`private class ImageProcessingTask extends AsyncTask<Bitmap, Void, Bitmap> {
    private ProgressDialog abhanDialog = null;
    private Bitmap returnedBitmap = null;

    @Override
    protected void onPreExecute() {
        returnedBitmap = null;
        abhanDialog = new ProgressDialog(AbhanActivity.this);
        abhanDialog.setMessage(getString(R.string.please_wait));
        abhanDialog.setCancelable(false);
        abhanDialog.show();
    }

    @Override
    protected Bitmap doInBackground(Bitmap... params) {
        final Bitmap sketched = AbhanSketch.createSketch(params[0]);
        final Bitmap gaussianBitmap = AbhanEffects.applyGaussianBlur(sketched);
        final Bitmap sepiaBitmap = AbhanEffects.sepiaTonnedBitmap(gaussianBitmap, 151, 0.71,
                0.71, 0.76);
        returnedBitmap = AbhanEffects.sharpenBitmap(sepiaBitmap, 0.81);
        return returnedBitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (abhanDialog != null && abhanDialog.isShowing()) {
            abhanDialog.cancel();
        }
        if (result != null) {
            mImageView.setImageBitmap(result);
            mImageView.buildDrawingCache();
            bmap = mImageView.getDrawingCache();
            storeImage(bmap);
            isImage = false;
            enableButton();
            final boolean isFileDeleted = Utils.deleteFile(mPath);
            if (DEBUG) {
                android.util.Log.i(TAG, "File Deleted: " + isFileDeleted);
            }
        }
    }
}

private void storeImage(Bitmap image) {
    ...please enter code here for image storing
}`
  • File pictureFile = getOutputMediaFile(); if (pictureFile == null) { Log.d(TAG, "Error creating media file, check storage permissions: ");// e.getMessage()); return; } try { FileOutputStream fos = new FileOutputStream(pictureFile); image.compress(Bitmap.CompressFormat.PNG, 90, fos); Toast.makeText(AbhanActivity.this, "stored", Toast.LENGTH_SHORT).show(); fos.close(); } catch (FileNotFoundException e) { Log.d(TAG, "File not found: " + e.getMessage()); } catch (IOException e) { Log.d(TAG, "Error accessing file: " + e.getMessage()); } – Hafiz Muhammad Afzal Nov 05 '16 at 07:10
  • i am using the above code for image storing purposes – Hafiz Muhammad Afzal Nov 05 '16 at 07:11
  • refer my answer http://stackoverflow.com/questions/37967711/how-to-take-screenshot-for-android-surface-view/37968834#37968834 , you have to render parent view in which your image view reside and take it as screenshots ,rest of the things are mention there. – Radhey Nov 05 '16 at 07:14
  • `storeImage(bmap);` ??? Why not storing the original bitmap? Like `storeImage(result);` ? – greenapps Nov 05 '16 at 09:56
  • Do you want to store the bitmaps pixels/bytes or create a jpg or png or what? – greenapps Nov 05 '16 at 09:57

2 Answers2

0

in your function write the following code

String path = Saveme(image,"image_name.jpg");
//path contains the full path to directory where all your images get stored internaly lolz but privately

for gallery
 Saveme(image,"my image","my image test for gallery save");

and the defination for the Saveme() function is following

private String Saveme(Bitmap bitmapImage, String img_name){
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
             // path to /data/data/yourapp/app_data/imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            // Create imageDir
            File mypath=new File(directory,img_name);

            FileOutputStream fos = null;
            try {           
                fos = new FileOutputStream(mypath);
           // Use the compress method on the BitMap object to write image to the OutputStream
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                try {
                  fos.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
            } 
            return directory.getAbsolutePath();
        }

in gallery images are displayed from media store so you need to store image in media store the following code can help you for this

public void Saveme(Bitmap b, String title, String dsc) 
{
MediaStore.Images.Media.insertImage(getContentResolver(), b, title ,dsc);    
}
Zain Ul Abidin
  • 2,019
  • 1
  • 12
  • 21
0

Here is your missing code inside a function

private void storeImage(Bitmap image) {

    File sdcard = Environment.getExternalStorageDirectory() ;

    File folder = new File(sdcard.getAbsoluteFile(), "YOUR_APP_DIRECTORY");
    if(!folder.exists())
        folder.mkdir();
    File file = new File(folder.getAbsoluteFile(), "IMG_" + System.currentTimeMillis() + ".jpg") ;
    if (file.exists())
        file.delete();

    try {
        FileOutputStream out = new FileOutputStream(file);

        bitmap = Bitmap.createScaledBitmap(bitmap, 400, (int) ( bitmap.getHeight() * (400.0 / bitmap.getWidth()) ) ,false);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

You can omit or edit this line

bitmap = Bitmap.createScaledBitmap(bitmap, 400, (int) ( bitmap.getHeight() * (400.0 / bitmap.getWidth()) ) ,false);

according to your need.

Waqas Ahmed Ansari
  • 1,653
  • 15
  • 29