如何创建一个目录,然后将图片保存到它的Andr​​oid [英] How to create a directory, and save a picture to it in Android

查看:125
本文介绍了如何创建一个目录,然后将图片保存到它的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写一个函数,它试图:


  1. 创建与用户名称的文件夹

  2. 保存JPG格式的里面
        文件夹

该文件夹的创建工作正常,但是当我尝试保存图片,它们都保存正确的名称,但他们并不在其预期的文件夹保存。换句话说,而不是包含了一堆包含一个画面的每个文件夹的文件夹中,我有一个包含了一堆空文件夹中的一个文件夹,和一堆图片所有的文件夹外(如果需要的话我可以澄清)的。

这是我的code:

 公共无效addToDir(列表<联系与GT;名单){        的for(int i = 0; I<则为list.size();我++){            串nameOfFolder = list.get(ⅰ).getName();
            位图currentBitmap = list.get(ⅰ).getBusiness_card();            字符串CONNAME = Environment.getExternalStorageDirectory()+文件分割符+MyApp的+文件分割符+
                    关联账户+文件分割符+ nameOfFolder;            文件conDir =新的文件(CONNAME);            如果(!conDir.mkdirs()){
                如果(conDir.exists()){
                }其他{
                    返回;
                }
            }            尝试{
                FOS的FileOutputStream =新的FileOutputStream(CONNAME +.JPG,真正的);
                currentBitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);                fos.flush();
                fos.close();
            }赶上(例外五){
                Log.e(MyLog,e.toString());
            }
        }
    }

我怀疑问题是与FileOutputStream中的路径,但我不知道如何设置它,这样它被设置为我刚刚创建的文件夹中。

大部分AP preciated


解决方案

  

这是如何定义mFileTemp


 字符串状态= Environment.getExternalStorageState();
文件mFileTemp;
如果(Environment.MEDIA_MOUNTED.equals(州)){
//这是这样的
//目录:任何文件夹名称/您可以添加这样的内部文件夹/您的照片name122412414124.jpg
mFileTemp =新的文件(Environment.getExternalStorageDirectory()+文件分割符+的任何文件夹名称+文件分割符+您可以添加这样的内部文件夹
        您的照片的名字+ System.currentTimeMillis的()+)JPG;
mFileTemp.getParentFile()mkdirs()。
}
其他{
mFileTemp =新的文件(getFilesDir()+的任何文件夹名称+
        文件分割符+myphotos)+文件分割符+profilephotos,您的照片的名字+ System.currentTimeMillis的()+)JPG;
mFileTemp.getParentFile()mkdirs()。


  

这是我如何保存所有图片


  {尝试
        。为InputStream的InputStream = getContentResolver()openInputStream(data.getData());
        FileOutputStream中的FileOutputStream =新的FileOutputStream(mFileTemp);
        copyStream(InputStream中,FileOutputStream中);
        fileOutputStream.close();
        inputStream.close();
    }赶上(例外五){
        Log.e(错误保存,建立出错临时形象,E);
    }


  

和copyStream方法


 公共静态无效copyStream(输入的InputStream,OutputStream的输出)抛出IOException
    字节[]缓冲区=新的字节[1024];
    INT读取动作;
    而((读取动作= input.read(缓冲液))!= - 1){
        output.write(缓冲液,0,读取动作);
    }
}

This is a function I have written that tries to:

  1. Create a folder with the users name
  2. Save a .jpg inside of that folder

The folder creation works fine, however when I try to save the pictures, they all save with the correct name, however they do not save in their intended folders. In other words, instead of having a folder containing a bunch of folders each containing one picture, I have one folder containing a bunch of empty folders, and a bunch of pictures all outside their folders (I can clarify if needed).

This is my code:

 public void addToDir(List<Contact> list){

        for(int i = 0; i < list.size(); i++){

            String nameOfFolder = list.get(i).getName();
            Bitmap currentBitmap = list.get(i).getBusiness_card();

            String conName = Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator +
                    "Connected Accounts" + File.separator + nameOfFolder;

            File conDir = new File(conName);

            if (!conDir.mkdirs()) {
                if (conDir.exists()) {
                } else {
                    return;
                }
            }

            try {
                FileOutputStream fos = new FileOutputStream(conName + ".jpg", true);
                currentBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                fos.flush();
                fos.close();
            } catch (Exception e) {
                Log.e("MyLog", e.toString());
            }
        }
    }

I suspect the problem is with the FileOutputStream path, but I am not sure how to set it so that it is set to the folder I just created.

Much appreciated

解决方案

This is how to define mFileTemp

String state = Environment.getExternalStorageState();
File mFileTemp;
if (Environment.MEDIA_MOUNTED.equals(state)) {
//this is like that 
//directory :  any folder name/you can add inner folders like that/your photo name122412414124.jpg
mFileTemp = new File(Environment.getExternalStorageDirectory()+File.separator+"any folder name"+File.separator+"you can add inner folders like that"
        , "your photo name"+System.currentTimeMillis()+".jpg");
mFileTemp.getParentFile().mkdirs();
}
else {
mFileTemp = new File(getFilesDir()+"any folder name"+
        File.separator+"myphotos")+File.separator+"profilephotos", "your photo name"+System.currentTimeMillis()+".jpg");
mFileTemp.getParentFile().mkdirs();

This is how i save any image

try {
        InputStream inputStream = getContentResolver().openInputStream(data.getData());
        FileOutputStream fileOutputStream = new FileOutputStream(mFileTemp);
        copyStream(inputStream, fileOutputStream);
        fileOutputStream.close();
        inputStream.close();
    } catch (Exception e) {
        Log.e("error save", "Error while creating temp image", e);
    }

And copyStream method

public static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}

这篇关于如何创建一个目录,然后将图片保存到它的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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