Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何制作名为“的文件夹”;“私人文件夹”;在画廊安卓4+;版本[以编程方式]_Android_Image_Insert_Directory_Gallery - Fatal编程技术网

Android 如何制作名为“的文件夹”;“私人文件夹”;在画廊安卓4+;版本[以编程方式]

Android 如何制作名为“的文件夹”;“私人文件夹”;在画廊安卓4+;版本[以编程方式],android,image,insert,directory,gallery,Android,Image,Insert,Directory,Gallery,我正在制作一个应用程序,在这个应用程序中,我想在gallery中创建一个名为“private folder”(当用户单击按钮“Create folder”)的文件夹,然后想在其中插入一个图像,比如说“mypic.png”。。。任何人都可以帮助plzzz您可以在外部内存中创建如下目录: File mydir = new File(Environment.getExternalStorageDirectory() + "/privatefolder/"); mydir.mkdirs(); 然后你可

我正在制作一个应用程序,在这个应用程序中,我想在gallery中创建一个名为“private folder”(当用户单击按钮“Create folder”)的文件夹,然后想在其中插入一个图像,比如说“mypic.png”。。。任何人都可以帮助plzzz

您可以在外部内存中创建如下目录:

File mydir = new File(Environment.getExternalStorageDirectory() + "/privatefolder/");
mydir.mkdirs();
然后你可以用不同的方式保存你的文件。这是其中之一,您已经有一个文件路径,并且希望将该图像复制到您的文件夹中:

String oldFilePath = "Your old FilePath";
String newFilePath = new File(Environment.getExternalStorageDirectory() + "/privatefolder/" + "Your image name.jpg");

File newFile = copyFile(oldFilePath , newFilePath);

public static File copyFile(String from, String to) {
    try {
        File sd = Environment.getExternalStorageDirectory();
        if (sd.canWrite()) {
            int end = from.toString().lastIndexOf("/");
            String str1 = from.toString().substring(0, end);
            String str2 = from.toString().substring(end+1, from.length());
            File source = new File(str1, str2);
            File destination = new File(to, str2);
            if (source.exists()) {
                FileChannel src = new FileInputStream(source).getChannel();
                FileChannel dst = new FileOutputStream(destination).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
            return destination;
        }

    } catch (Exception e) {
        return null;
    }
    return null;
}
另外,不要忘记在清单中添加此权限:

< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用了一些答案代码。

你所说的
gallery
是什么意思?。。。内置图库应用程序?只需在外部或可移动媒体的某处创建一个文件夹“私人文件夹”,并在其中放入一些图片。Gallery应用程序最终会找到您的文件夹。@SaDeGH\u F我是说内置Gallery应用程序。@greenapps您可以共享在外部或可移动媒体中创建文件夹的代码。请先尝试并显示您的代码。这个网站上有很多例子。使用文件类。谢谢老兄,我得到了一半的答案。。。您告诉我一种将图像从源移动到目标的方法,但我想将图像从“drawables”插入到私人文件夹,例如我想移动@/drawable/mypic.png。。你能告诉我吗plz@HassanAkhtar我按照你的要求编辑了代码。希望有帮助;)
//first get a bitmap from your image. if your image name is "photo" do it like this
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.photo);

//then creat your directory. and then copy your image (in this code image format is PNG)
File mydir = new File(Environment.getExternalStorageDirectory() + "/privatefolder/");
mydir.mkdirs();
File file = new File(mydir, "photo.PNG");
FileOutputStream outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();