Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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/2/sharepoint/4.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 将图像存储和检索到SD卡中_Android_Android Sdcard - Fatal编程技术网

Android 将图像存储和检索到SD卡中

Android 将图像存储和检索到SD卡中,android,android-sdcard,Android,Android Sdcard,我有一组图像URL。我把它下载到位图。现在我想将这些图像存储到SD卡/项目文件夹中。如果我没有这样的文件,我必须创建它。我现在所做的是: String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; File file = new File(path, imageName); if(!file.exists()) { file.mkdir(); try {

我有一组图像URL。我把它下载到位图。现在我想将这些图像存储到SD卡/项目文件夹中。如果我没有这样的文件,我必须创建它。我现在所做的是:

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, imageName);
if(!file.exists()) {
    file.mkdir();
    try {
        fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), "file://"
            + file.getAbsolutePath(), file.getName(), file.getName());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}   

但我不会将图像插入SD卡。我的代码有什么问题?请回复。提前感谢。

尝试使用以下代码:

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    String fileName = edtNameImage.getText().toString().trim();//this can be changed
    if (fileName.equalsIgnoreCase("")) {
        Toast.makeText(context, "Fields cannot be left blank",
                Toast.LENGTH_SHORT).show();
        return false;
    }
    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + fileName);
    // write the bytes in file
    FileOutputStream fo;
    try {
        file.createNewFile();
        fo = new FileOutputStream(file);//snapshot image is the image to be stored.

        if (snapShotImage!=null)
        {
            snapShotImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        }else{
            return false;
        }
        fo.write(bytes.toByteArray());
        // ChartConstants.IMAGE_STORAGE++;
        fo.flush();
        fo.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return true;
您还可以使用一些额外的代码行检查名称中的重复性。
如果有帮助,请告诉我。

尝试使用以下代码:

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    String fileName = edtNameImage.getText().toString().trim();//this can be changed
    if (fileName.equalsIgnoreCase("")) {
        Toast.makeText(context, "Fields cannot be left blank",
                Toast.LENGTH_SHORT).show();
        return false;
    }
    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + fileName);
    // write the bytes in file
    FileOutputStream fo;
    try {
        file.createNewFile();
        fo = new FileOutputStream(file);//snapshot image is the image to be stored.

        if (snapShotImage!=null)
        {
            snapShotImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        }else{
            return false;
        }
        fo.write(bytes.toByteArray());
        // ChartConstants.IMAGE_STORAGE++;
        fo.flush();
        fo.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return true;
您还可以使用一些额外的代码行检查名称中的重复性。 如果有帮助,请告诉我

  • 您是否已确保在Manifest.xml文件中设置了写入/读取外部存储设备的相应权限

  • 此外,代码是否存在上述任何异常?打印比stacktrace更多的说明性消息。大概是这样的:

    试试{
    }catch(filenotfound异常){
    Log.v(this.toString(),“块中捕获异常”);
    }

  • 或者在这句话里

    HTH
    斯利拉姆

  • 您是否已确保在Manifest.xml文件中设置了写入/读取外部存储设备的相应权限

  • 此外,代码是否存在上述任何异常?打印比stacktrace更多的说明性消息。大概是这样的:

    试试{
    }catch(filenotfound异常){
    Log.v(this.toString(),“块中捕获异常”);
    }

  • 或者在这句话里

    HTH

    Sriram与代码有关的问题:
    1) 您正在使用文件名创建目录。改为只使用“路径”尝试mkdir() 2) 仅将不带“file://”的“file.getAbsolutePath()”传递给insertImage()函数
    3) 有一个替代api,您无需在本地为其创建更多文件。将位图直接传递给该对象


    希望这有帮助。

    代码问题:
    1) 您正在使用文件名创建目录。改为只使用“路径”尝试mkdir() 2) 仅将不带“file://”的“file.getAbsolutePath()”传递给insertImage()函数
    3) 有一个替代api,您无需在本地为其创建更多文件。将位图直接传递给该对象

    希望这有帮助