简体   繁体   English

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

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

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. 然后,该应用程序将图像保存到设备,然后将文件路径存储到sqlite数据库。 It then later uses the Picasso library to load the image from the file path. 然后,它稍后使用Picasso库从文件路径加载图像。 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. 我希望它可以更快地加载图像。

Here is my code: 这是我的代码:

Method that gets the result of the Intent for user to choose image 获取意图结果供用户选择图像的方法

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

    }
}

Async Task to do the image saving 异步任务进行图像保存

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

 }

How I load the file path 我如何加载文件路径

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

Any advice would be greatly appreciated. 任何建议将不胜感激。 Thank you. 谢谢。

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 一种选择是通过WeakRefrence缓存图像,这样可以将图像保留在内存中,如果图像不在内存中,则可以从sdcard加载

i think below links can help you 我认为以下链接可以帮助您

weakReference and other weak reference for cache 缓存的 weakReference和其他弱引用

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

相关问题 从URL Android加载大量图像的最有效方法 - Most efficient way to load a lot of images from URL Android 在Android中下载Google云端硬盘图像的最有效方法是什么? - What is the most efficient way to download Google Drive images in Android? 在Android中保存游戏状态的最有效方法是什么? - What is the most efficient way to save game status in Android? 在Android中运行Cursor的最有效方法是什么? - What's the most efficient way to run over a Cursor in Android? Android:保存 int 列表的最有效方法 - Android: Most efficient way to save a list of int 在Android中加载/保存属性文件的最便捷方法是什么? - What is the most convenient way to load/save property files in Android? 在Android中显示表格的最有效方法是什么 - What is the most efficient way to display a table in Android 使用Android相机的最有效方法是什么? - What is the most efficient way to work with the Android camera? 在Android的“ ActionBar”中控制“ MenuItem”的最佳方式(最有效或最常规)是什么? - What is the best (most efficient or conventional) way to control “MenuItem”s in Android's “ActionBar”? 在Android中的本机代码和Java代码之间传输数据/数据包的最有效方法是什么 - What's the most efficient way to transfer data/packets between native code and Java code in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM