【问题标题】:android , downloaded file image does not exist?android,下载的文件图片不存在?
【发布时间】:2015-08-17 01:19:18
【问题描述】:

我是 android 新手,我正在使用 Android 异步 Http 客户端库从服务器下载图像文件 但是当我尝试下载图像时,状态是成功但图像没有下载到内部存储中!!

这是我的代码

download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String url = getIntent().getStringExtra("url");

            AsyncHttpClient client = new AsyncHttpClient();
            client.get(url, new FileAsyncHttpResponseHandler(FullImageActivity.this) {
                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
                    Toast.makeText(FullImageActivity.this, "ERROR", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, File response) {
                    Toast.makeText(FullImageActivity.this, "DONE", Toast.LENGTH_SHORT).show();



                }
            });
        }
    });

请帮忙!?

【问题讨论】:

    标签: android image asynchronous storage


    【解决方案1】:

    我没有使用该库,但基于回调定义和文档 - 该文件不会自动保存在设备上的任何位置。回复会在 byte[] response 中返回,您有责任保存或以您想要的方式使用它。

    【讨论】:

    • 听起来不错,,, 但我怎样才能保存它?!如果你知道,请帮助我
    • 没关系。我回答的时候太累了。我正在查看AsyncHttpResponseHandler 的回调,它返回一个字节[]。你实际上是在使用FileAsyncHttpResponseHandler,你应该可以在文件中得到响应,而不是字节[]。
    【解决方案2】:

    使用Picasso 进行图片加载和下载。 http://square.github.io/picasso/

    编辑: 如果要下载并保存到外部存储,请先下载图像:

    try {
            URL url = new URL(requestUrl);
            URLConnection conn = url.openConnection();
            bitmap = BitmapFactory.decodeStream(conn.getInputStream());
        } catch (Exception ex) {
    }
    

    然后保存到sdCard:

    void saveToSdCard(Bitmap bitmap, String filename) {
    
        File sdcard = Environment.getExternalStorageDirectory() ; 
    
        File folder = new File(sdcard.getAbsoluteFile(), ".YOUR_DIRECTORY"); //Replace with the name of the directory you want.
        folder.mkdir(); 
        File file = new File(folder.getAbsoluteFile(), filename + ".jpg"); //Assuming your image's extension is '.jpg' .
        if (!file.exists()) {
    
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); //Again, assuming it's '.jpg' .
            out.flush();
            out.close();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 我用它在 imageview 中加载图像.. 但是我怎样才能以高分辨率将该图像下载到内部存储(画廊)!?你能帮我举个例子或代码sn-p吗?!
    • 您是否在清单中添加了:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    • 是的......我想如果我之前没有添加它会导致权限崩溃!
    【解决方案3】:

    请参阅此 Android 库:http://square.github.io/picasso/

    使用非常简单:

    ImageView img = (ImageView)findViewById(R.id.imageView1);
    Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(img);
    

    【讨论】:

    • 它加载 url 图像到 imageview .. 我想下载内部存储中的图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多