ACTION_MEDIA_MOUNTED没有刷新图库

时间:2015-02-26 11:47:16

标签: android android-intent android-mediascanner

我从服务器获取图像并在我的应用程序中显示它,我下载该图像并且下载工作正常,但是当我检查我的图库图像没有显示在那里时,然后在开发工具 - Media Scanner中扫描我的SD卡再次检查我的画廊,然后图像显示..所以我怎么能解决它..即使我尝试了三星手机,但设备我需要重新启动我的设备...以下是我的代码片段......

 public class bBusinessCardDL extends Activity{


String[] NAMES = new String[1];
String[] CurID = new String[1];
String[] Detail = new String[1];
String[] Photo = new String[1];
ListView listview;
String BCard;

ImageView image;

Button btnDownload;
ProgressDialog mProgressDialog;
private String Id;
private ImageView bcks;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_bu_dl);
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + "/mnt/sdcard/")));
    bcks=(ImageView)findViewById(R.id.bck_from_bcard);

    bcks.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intss=new Intent(bBusinessCardDL.this,FirstPage.class);
            startActivity(intss);
        }
    });

    Id=this.getIntent().getStringExtra("userids");
    System.out.println("checkd advertisement "+Id);
    FillData();

    btnDownload = (Button) findViewById(R.id.btnDownload);
    btnDownload.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            mDownloadAndSave();
            Toast msgd = Toast.makeText(getBaseContext(),
                    "Business card Downloaded..!", Toast.LENGTH_LONG);
            msgd.show();

        }
    });


}

public void mDownloadAndSave() { 


    File f = new File("/mnt/sdcard/" + Id
            + ".jpg");


    //"/mnt/sdcard/"
    InputStream is;
    try {
        is = new URL(BCard).openStream();

        // Set up OutputStream to write data into image file.
        OutputStream os = new FileOutputStream(f);

        CopyStream(is, os);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


MediaScannerConnection.scanFile(this, new String[] { "ur_file_path" },
        null,
 new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {

}
}); 
}

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 2048;
    try {
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
    } catch (Exception ex) {

    }
}

public static String getJsonFromServer(String url) throws IOException {

    BufferedReader inputStream = null;

    URL jsonUrl = new URL(url);
    URLConnection dc = jsonUrl.openConnection();

    dc.setConnectTimeout(5000);
    dc.setReadTimeout(5000);

    inputStream = new BufferedReader(new InputStreamReader(
            dc.getInputStream()));

    // read the JSON results into a string
    String jsonResult = inputStream.readLine();
    return jsonResult;
}

static class ViewHolder {
    TextView VHName;
    ImageView VHPhoto;
    int position;
}

public void FillData() {
    String url = "";



    url = "http://www.asdffsfd.com/web-service/b_card.php?user_id="
            + Id;
    String jsonString;
    jsonString = "";

    try {
        jsonString = getJsonFromServer(url);
    } catch (IOException e) {
    }

    BCard = "";
    try {
        JSONArray earthquakes = new JSONArray(jsonString);

        NAMES = new String[earthquakes.length()];
        Photo = new String[earthquakes.length()];

        for (int i = 0; i < earthquakes.length(); i++) {
            JSONObject e = earthquakes.getJSONObject(i);
            NAMES[i] = e.getString("b_card");
            BCard = "http://" + e.getString("b_card");

        }

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    BCard = BCard.replace("\\", "");
    BCard = BCard.replace(" ", "%20");
    ImageView i = (ImageView) findViewById(R.id.BUCARD);

    Log.d("Bcard", BCard);
    try {

        Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                BCard).getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

1 个答案:

答案 0 :(得分:1)

保存图像后,使用以下代码扫描文件:

MediaScannerConnection.scanFile(this, new String[] { f.getAbsolutePath()},
                    null,
         new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {

            }
            });