如何将位图存储到android中的内部存储中

时间:2016-11-05 07:08:57

标签: android bitmap android-imageview file-handling

我知道这是一个非常基本的问题,但我坚持要解决这个问题。我正在制作图像素描移动应用程序我已完成所有工作现在我只想将结果位图图像存储到内部存储器中。我创建了一个方法“mageStore()”用于图像存储目的,请在那里编写代码。我将非常感谢你。

`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
}`

2 个答案:

答案 0 :(得分:0)

在您的函数中

编写以下代码

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");

并且Saveme()函数的定义如下

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();
        }
图库中的

是从媒体商店中显示的,因此您需要将图像存储在媒体商店中,以下代码可以帮助您实现此目的

public void Saveme(Bitmap b, String title, String dsc) 
{
MediaStore.Images.Media.insertImage(getContentResolver(), b, title ,dsc);    
}

答案 1 :(得分:0)

这是函数中缺少的代码

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();
    }
}

您可以省略或编辑此行

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

根据您的需要。