简体   繁体   English

如何在Android中将所有位图图像发送到SD卡

[英]How to send all the bitmap images to sdcard in android

To share the image via MMS the code is working. 要通过彩信共享图像,代码正在运行。 What i have done is, converted drawable image to bitmap and saved in sd card. 我所做的是,将可绘制的图像转换为位图并保存在SD卡中。 and retrieved to send through intent, this is for one image, i'm struck in sending all the images in an array to sd card. 并检索通过意图发送,这是一个图像,我很惊讶地将数组中的所有图像发送到SD卡。 in below code i have placed the images in an array from drawable and converted all the 4 images to bitmap, how to send this 4 images to sdcard ? 在下面的代码中,我将图像从drawable放置在数组中,并将所有4张图像都转换为位图,如何将这4张图像发送到sdcard?

 private void doSendIntent(String subject, String text) {
      try {
    Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
       sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); 
   sendIntent.setType("image/png");

   ArrayList<Integer> myImageList = new ArrayList<Integer>();
    myImageList.add(R.drawable.gicon);
    myImageList.add(R.drawable.ic_launcher);
    myImageList.add(R.drawable.loadin);
    myImageList.add(R.drawable.splash);


    Bitmap bbicon;
    bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(),
            R.drawable.gicon);
    bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(),
            R.drawable.ic_launcher);
    bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(),
            R.drawable.loadin);
    bbicon = BitmapFactory.decodeResource(this.cordova.getActivity().getResources(),
            R.drawable.splash);


    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();          
    OutputStream outStream = null;

    File f = new File(extStorageDirectory + "/Download/",
            "gicon.png");
    try {
        outStream = new FileOutputStream(f);
        bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();

         System.out.println("ssssssssssssssssss");
    } catch (Exception e) {
    }
    }

    //RETRIEVING IMAGES FROM SDCARD

    File fl = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/Download/", "gicon.png");

    Uri uri = Uri.fromFile(fl);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    this.cordova.startActivityForResult(this, sendIntent, 0); 

      }
      catch(Exception e) 
      {
       System.out.println(" no error here");

       e.printStackTrace();
      }

Use following code. 使用以下代码。

private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
    files = assetManager.list("");
} catch (IOException e) {
    Log.e("tag", e.getMessage());
}
for(String filename : files) {
    InputStream in = null;
    OutputStream out = null;
    try {
      in = assetManager.open(filename);
      out = new FileOutputStream("/sdcard/" + filename);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
    } catch(Exception e) {
        Log.e("tag", e.getMessage());
    }       
  }
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
   byte[] buffer = new byte[1024];
   int read;
   while((read = in.read(buffer)) != -1){
   out.write(buffer, 0, read);
 }
}

Take below permission from Manifest file 从清单文件获取以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM