在默认图库中打开缓存位图的更快方法

发布于 2024-12-12 02:37:18 字数 2214 浏览 0 评论 0原文

我编写了一些代码,当单击列表视图时,该位置的图像将存储到外部存储器,然后发送文件路径字符串,以便在默认图库中查看图像。唯一的问题是它需要很长的时间(我说的是我的 Thunderbolt 10 多秒)。

我尝试过的: 1. 将位图存储在内存中 2. 降低位图质量

代码如下:

@Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    if(position>0){
                        Bitmap bmp =adapter.getBitmap(adapter.getData(position-1));
                        if(bmp!=null){
                            //String path = context.getCacheDir().getAbsolutePath() + "/view.png"; 
                            //File f = new File(context.getCacheDir().getAbsolutePath(),"MemeCache");
                            //if(!f.exists())
                            //    f.mkdirs();
                            String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/view.png"; 
                            Toast.makeText(context, "opening in gallery", Toast.LENGTH_SHORT).show();
                            File file = new File(path); 
                            FileOutputStream fos = null;
                            try {
                                fos = new FileOutputStream(file);
                                bmp.compress(CompressFormat.PNG, 100, fos ); 
                                fos.close(); 
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            Intent intent = new Intent();
                            intent.setAction(android.content.Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
                            //intent.setDataAndType(Uri.fromFile(f), "image/png");
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(context, arg0.getItemAtPosition(position).toString() +"is HaAAACkSS!!!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });

I have written a bit of code that when the listview is clicked, the image at that location is stored to external memory, then the file path string is sent with the intent to view the image in the default gallery. The only problem is that it takes a seriously long amount of time (I'm talking 10+ seconds on my thunderbolt).

What I haved tried:
1. Storing the bitmap on internal memory
2. Lowering the quality of the bitmap

Here is the code:

@Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    if(position>0){
                        Bitmap bmp =adapter.getBitmap(adapter.getData(position-1));
                        if(bmp!=null){
                            //String path = context.getCacheDir().getAbsolutePath() + "/view.png"; 
                            //File f = new File(context.getCacheDir().getAbsolutePath(),"MemeCache");
                            //if(!f.exists())
                            //    f.mkdirs();
                            String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/view.png"; 
                            Toast.makeText(context, "opening in gallery", Toast.LENGTH_SHORT).show();
                            File file = new File(path); 
                            FileOutputStream fos = null;
                            try {
                                fos = new FileOutputStream(file);
                                bmp.compress(CompressFormat.PNG, 100, fos ); 
                                fos.close(); 
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            Intent intent = new Intent();
                            intent.setAction(android.content.Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
                            //intent.setDataAndType(Uri.fromFile(f), "image/png");
                            activity.startActivity(intent);
                        }else{
                            Toast.makeText(context, arg0.getItemAtPosition(position).toString() +"is HaAAACkSS!!!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2024-12-19 02:37:18

有些事情可能会有所帮助:

1)在代码中添加一些计时器或仪器,以准确查看您一直在哪里花费:将位图保存到 SD 卡,启动意图,或完全其他的东西。

2) 添加计时器并测量性能后,您可以查看缩小图像或将图像存储在其他位置是否有帮助,如果有帮助,效果有多大。在某些设备上,内部存储位于 SD 卡本身上。

3) 根据您的程序,您可能需要考虑将文件预先保存到 SD 卡(可能在后台),以便在用户尝试查看它们时它们可能已经保存画廊。

Some things that might help:

1) add some timers or instrumentation to you code to see exactly where you're spending all the time: saving the bitmap to the sdcard, starting the intent, or something else entirely.

2) Once you've added timers and can measure the performance, you can see if shrinking the image or storing the image in a different place helps and if so how much. On some devices, internal storage is on the sdcard itself.

3) depending on your program, you might want to consider pre-saving the files to the sdcard (possibly in the background) so that they are probably already saved by the time the user tries to view them in the gallery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文