使用毕加索下载时无法在SD卡中看到图像

时间:2016-01-26 00:28:34

标签: android

抱歉我的英语不好。 我有一个问题,我通过使用毕加索库从url下载图像,然后将其存储在SD卡中,但我无法在SD卡中看到它,我只能在重新启动手机时看到它,这是我的代码,我做的是什么错误???请帮助我!!!

public class Image extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_show_image);
    Intent it = getIntent();
    Bundle bundle = it.getExtras();
    final String link = bundle.getString("link");
    final ImageView ivView = (ImageView) findViewById(R.id.ivView);
    Picasso.with(getApplicationContext()).load(link).into(ivView);
    Button btSetWallpaper = (Button) findViewById(R.id.btSetWallpaper);
    btSetWallpaper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                downloadImage(link);
        }
    });
}
public void downloadImage(final String link){
    Picasso.with(getApplicationContext()).load(link).into(new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(Environment.getExternalStorageDirectory().getPath() + "/abc.jpg");
                    try {
                        file.createNewFile();
                        FileOutputStream outputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                        outputStream.close();
                    }
                    catch (Exception e){
                    e.getStackTrace();

                    }



                }
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "Success" + Environment.getExternalStorageDirectory().getPath(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            Toast.makeText(getApplicationContext(), "Load Fail", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {

        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

您需要告诉Android Media Library已添加媒体文件。

Intent intent =
      new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

将文件替换为您保存的文件,该文件应显示在Android媒体库中,而无需重新启动手机。

答案 1 :(得分:0)

您需要告诉系统运行媒体扫描程序,然后该文件才会显示在库中。可以按如下方式完成:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
              Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

OR

MediaScannerConnection.scanFile(this,
          new String[] { filepath }, null,
          new MediaScannerConnection.OnScanCompletedListener() {
      public void onScanCompleted(String path, Uri uri) {
         // your code here
      }
 });