【问题标题】:Saving images dynamically from application to SD card将图像从应用程序动态保存到 SD 卡
【发布时间】:2012-08-18 17:30:43
【问题描述】:

我正在制作一个画廊,我在其中从资产文件夹加载(在运行时)图像。现在我想在点击事件时将图像保存到 SD 卡。

例如: 当应用启动时,用户看到图像,他们可以滚动浏览图像并查看它们(这部分完成)。问题是图片在我自己的画廊视图中动态加载。我没有硬编码它们。

我想将它保存到 SD 卡。但我没有图像的硬编码路径。可以有任意数量的图像。

 private void CopyAssets() {
        AssetManager assetManager = getAssets();
        InputStream in=null;
    String[] files = null;
        try {
            files = assetManager.list("image");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for(String filename : files) {


                try {
                    in = assetManager.open(filename);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        try {
                String dirName = Environment.getExternalStorageDirectory().toString(); 
                File newFile = new File(dirName); 
                newFile.mkdirs(); 

        OutputStream out = new FileOutputStream(newFile);
        System.out.println("in tryyyy");

                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
        }

我尝试了上述方法,我不想将所有图像复制到 SD 卡。但只有用户从画廊中选择的那个太动态了。因为会有很多图像。对每个图像路径进行硬编码会很困难。

在 Android 中有什么方法可以获取当前图像路径或字符串中的 URI? View v = this.getCurrentFocus(); 是什么?它返回什么?

【问题讨论】:

  • 您是否在图库中显示可绘制文件夹中的图像?
  • 不,我正在共享 Assets 文件夹中的图像

标签: android android-sdcard


【解决方案1】:

a Gallery 扩展自 AdapterView ,就像在 adapterView 上一样,您可以添加一个监听器,用于当项目被选中时。

你知道哪个项目被选中,使用它来复制你想要的图像到 sd 卡。

为了更好地了解如何为 adapterView 实现适配器,请观看“the world of listView”视频。您可能希望将路径放入 viewHolder(取决于您的代码和设计)。

【讨论】:

    【解决方案2】:

    这是我创建的一种方法,可让您将图像(位图)保存到内存中。参数需要位图对象和该对象的文件名。

    public void writeBitmapToMemory(String filename, Bitmap bitmap) {
            FileOutputStream fos;
            // Use the compress method on the Bitmap object to write image to the OutputStream
            try {
                fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
                // Writing the bitmap to the output stream
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
    
            } 
            catch (FileNotFoundException e) {
                e.printStackTrace();
    
    
            } 
            catch (IOException e) {
                e.printStackTrace();
    
    
            }
    
        }
    

    我希望这会有所帮助。

    【讨论】:

    • 如果我通过这一行 Bitmap bitmap=gallery.getDrawingCache();是可能的???并且fileName是保存图像的新文件夹?
    • 如果gallery.getDrawingCache() 返回一个位图对象并且文件名是一个字符串(例如“picture1”),那么是的。
    • 嘿,你能解释一下上面代码中的文件名(它实际上代表什么?)吗?是否是sd卡中要从asset保存图片的文件夹的文件名?
    • 文件名与您正在保存的位图“链接”。
    • 当我通过这个 writeBitmapToMemory("file://"+getPackageName()+"/"+ getAssets()+"/", bitmap);我得到 08-18 23:43:59.189: E/AndroidRuntime(22833): FATAL EXCEPTION: main 08-18 23:43:59.189: E/AndroidRuntime(22833): java.lang.IllegalArgumentException: File file://com .sample.galleryprojectnew/android.content.res.AssetManager@4051e648/ 包含路径分隔符我的图片在 assets-->image........请更正我的文件路径
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多