Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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
将下载的文件保存到java/android内部存储器失败_Java_Android_Save_Local Storage - Fatal编程技术网

将下载的文件保存到java/android内部存储器失败

将下载的文件保存到java/android内部存储器失败,java,android,save,local-storage,Java,Android,Save,Local Storage,我有一个应用程序谁必须下载一些生成的图像(PNG) 我尝试了标准方法ImageDownloader extends AsyncTask,doInBackground()检索图像,而onPostExecute()将尝试将其保存到内部图像 (部分)代码如下: public class HandleImages extends AppCompatActivity { String filename = ""; public boolean saveImageToInternalStorage(Bit

我有一个应用程序谁必须下载一些生成的图像(PNG)

我尝试了标准方法
ImageDownloader extends AsyncTask
doInBackground()
检索图像,而
onPostExecute()
将尝试将其保存到内部图像

(部分)代码如下:

public class HandleImages extends AppCompatActivity {
String filename = "";

public boolean saveImageToInternalStorage(Bitmap image) {
    try {
        FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);

        image.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public Bitmap retrieveImage(String url){
    ImageDownloader task = new ImageDownloader();
    Bitmap image =  Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);

    try {
        image = task.execute(url).get();
    } catch (InterruptedException e) {
        MainActivity.debb("InterruptedException - " + e.getMessage() + " in " + new Object(){}.getClass().getEnclosingMethod().getName());
        e.printStackTrace();
    } catch (ExecutionException e) {
        MainActivity.debb("ExecutionException - " + e.getMessage() + " in " + new Object(){}.getClass().getEnclosingMethod().getName());
        e.printStackTrace();
    }
    return image;
}


public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        try {
            String[] filenames = urls[0].split("/");
            filename = filenames[filenames.length-1] + ".jpg";

            URL url = new URL(urls[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.connect();
            InputStream inputStream = connection.getInputStream();

            return BitmapFactory.decodeStream(inputStream);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap != null)
            saveImageToInternalStorage(bitmap);
    }
}
}
公共类HandleImage扩展了AppCompative活动{
字符串filename=“”;
公共布尔值saveImageToInternalStorage(位图图像){
试一试{
FileOutputStream fos=openFileOutput(文件名,Context.MODE\u PRIVATE);
image.compress(Bitmap.CompressFormat.PNG,100,fos);
fos.close();
返回true;
}捕获(例外e){
e、 printStackTrace();
返回false;
}
}
公共位图检索图像(字符串url){
ImageDownloader任务=新建ImageDownloader();
位图图像=Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888);
试一试{
image=task.execute(url.get();
}捕捉(中断异常e){
MainActivity.debb(“InterruptedException-”+e.getMessage()+“+new Object(){}.getClass().GetEnclosuringMethod().getName())中的“+e.getMessage()”;
e、 printStackTrace();
}捕获(执行例外){
MainActivity.debb(“ExecutionException-”+e.getMessage()+”在“+new Object(){}.getClass().GetEnclosuringMethod().getName()”中);
e、 printStackTrace();
}
返回图像;
}
公共类ImageDownloader扩展异步任务{
@凌驾
受保护位图doInBackground(字符串…URL){
试一试{
字符串[]文件名=URL[0]。拆分(“/”;
filename=文件名[filenames.length-1]+“.jpg”;
URL=新URL(URL[0]);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream InputStream=connection.getInputStream();
返回BitmapFactory.decodeStream(inputStream);
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(位图){
onPostExecute(位图);
if(位图!=null)
将图像保存到内部存储器(位图);
}
}
}
我得到的错误是:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String,int)”

似乎
FileOutputStream fos=openFileOutput(..)
失败了,但不知道为什么

还尝试在文件名前添加路径(
sdCard.getPath()+“/”+
)。正如预期的那样,这并没有产生任何影响

图像还可以,我可以在浏览器中看到它们。也尝试上传图片-而不是生成的,同样的结果

这很奇怪,有人知道吗

谢谢

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        } 
        return directory.getAbsolutePath();
    }
希望此功能对您有所帮助。如果这对你没有帮助,尽管问吧