什么样的Andr​​oid的保存和载入图像的最有效方法是什么? [英] Android what's the most efficient way to save and load images?

查看:136
本文介绍了什么样的Andr​​oid的保存和载入图像的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,用户无论从云,内部存储或外部存储选取图像的应用程序。该应用程序,然后保存图像的设备,然后存储的文件路径SQLite数据库。再后来使用了毕加索库从文件路径加载图像。我的问题是,当它从文件路径载入图像,它加载它极其缓慢。保存图像后,可能需要一分钟,终于显示出来。

I have an app where the user picks an image from either the cloud, internal storage or external storage. The app then saves the image to the device and then stores the file path to a sqlite database. It then later uses the Picasso library to load the image from the file path. My problem is that when it goes to load the image from the file path, it loads it extremely slow. After saving the image, it takes maybe a minute to finally display it.

我的问题是:什么是保存和加载由用户选择图像的最有效方式。我想它更快加载图像。

My question is: What is the most efficient way to save and load images chosen by a user. I would like it to load the images faster.

下面是我的code:

方法获取意向用户的结果来选择图片

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

任何意见将是极大的AP preciated。谢谢你。

Any advice would be greatly appreciated. Thank you.

推荐答案

一个选项将是这样,你就可以保持图像在内存thorugh WeakRefrence缓存图像,如果图像是不是在内存中,然后从SD卡装入

one option would be to cache image thorugh WeakRefrence in that way you can keep image in memory and if image is not in memory then load from sdcard

我想下面的链接可以帮助你。

i think below links can help you

了WeakReference
和缓存其他弱引用

weakReference and other weak reference for cache

这篇关于什么样的Andr​​oid的保存和载入图像的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆