繁体   English   中英

Android最有效的保存和加载图像的方法是什么?

[英]Android what's the most efficient way to save and load images?

我有一个应用程序,用户可以在其中从云,内部存储或外部存储中选择图像。 然后,该应用程序将图像保存到设备,然后将文件路径存储到sqlite数据库。 然后,它稍后使用Picasso库从文件路径加载图像。 我的问题是,当它打算从文件路径加载图像时,加载速度非常慢。 保存图像后,可能需要一分钟才能最终显示它。

我的问题是:保存和加载用户选择的图像的最有效方法是什么。 我希望它可以更快地加载图像。

这是我的代码:

获取意图结果供用户选择图像的方法

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) 
    {

        if(requestCode == 1)
        {
            try {
                if (bitmap != null)
                {
                    bitmap.recycle();
                }

                InputStream stream = getContentResolver().openInputStream(data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                stream.close();

                Picasso.with(getBaseContext()).load(data.getData()).fit().centerInside().into(imageButton);
                imageButton.setBackground(null);

            } 
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        pictureSelected = true;

        SaveImageTask saveBitmap = new SaveImageTask(bitmap);
        saveBitmap.execute();

    }
}

异步任务进行图像保存

    private class SaveImageTask extends AsyncTask<Void, Integer, Boolean> {

    private Bitmap bitmap;

    SaveImageTask(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    @Override
    protected Boolean doInBackground(Void...params) {

        // Create a media file name
        Calendar c = Calendar.getInstance();
        String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(c.getTime());
        String mImageName = "MT_"+ timeStamp +".jpg";
        String albumName = "My App";

        File file = null;

        String state = Environment.getExternalStorageState();

        // If there is external storage, save it in the pictures album. If not, save on internal storage
        if(Environment.MEDIA_MOUNTED.equals(state))
        {
            file = new File(addEdit.this.getExternalFilesDir(
                    Environment.DIRECTORY_PICTURES), albumName);
            if(!file.mkdirs())
            {
                file = new File(addEdit.this.getFilesDir(), mImageName);
            }
        }
        else
        {
            file = new File(addEdit.this.getFilesDir(), mImageName);
        }


        OutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file);
            fOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        try {
            FileOutputStream fos = new FileOutputStream(file);
            filePath = file.getAbsolutePath();
            Log.v("Filepath", filePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            return false;
        }
        catch(IOException e)
        {
            e.printStackTrace();
            return false;
        }


        return true;

     }

     protected void onProgressUpdate(Integer... progress) {
     }

     protected void onPostExecute(Boolean success) {
     }

 }

我如何加载文件路径

if(person.getImage() != null)
    {
        //convert byte to bitmap take from contact class
        File imgFile = new File (person.getImage());
        if(imgFile.exists())
        {
            Picasso.with(getBaseContext()).load(imgFile).fit().centerInside().into(imageView);
        }
    }

任何建议将不胜感激。 谢谢。

一种选择是通过WeakRefrence缓存图像,这样可以将图像保留在内存中,如果图像不在内存中,则可以从sdcard加载

我认为以下链接可以帮助您

缓存的 weakReference和其他弱引用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM