在android中保存和发送时的图像大小差异

时间:2016-04-08 04:31:45

标签: android android-intent android-bitmap

我在activity1中的图像被保存在大小为5.8kb的内部存储器中,并且我使用额外的意图流从同一活动发送相同的图像,并且在接收器端变为15kb。这背后的逻辑是什么?

Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setData(Uri.parse("mailto:"));
        intent.setType("imge/png");
        intent.putExtra(Intent.EXTRA_EMAIL, to); //user entered email address in edittext view.
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.putExtra(Intent.EXTRA_TEXT, "Message");
        intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
        startActivity(intent);
        saveImage(bmap);

    public void saveImage(Bitmap bitmap){
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/My Folder");
    String receiverN = receiverName.getText().toString();

    myDir.mkdirs();
    //name convention--------------------------------------------->>
    Calendar c = Calendar.getInstance();
    String month, day, year, hour, minute, second;
    month = ""+ (c.get(Calendar.MONTH)+1);
    day = "" + c.get(Calendar.DAY_OF_MONTH);
    year = "" + c.get(Calendar.YEAR);
    hour = ""+c.get(Calendar.HOUR_OF_DAY);
    if(hour.equals("0")) hour = "0"+hour;
    minute = "" + c.get(Calendar.MINUTE);
    int seconds = c.get(Calendar.SECOND);
    if (seconds<10) second = "0"+ seconds;
    else second = ""+seconds;

    String fname = receiverN + "-" + hour + ":" + minute + ":" + second + "_"  + month + "-" + day + "-" + year +".png";
    //name convention ends----------------------------------------->>

    File file = new File (myDir, fname);


    if (file.exists ()) file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
        String[] paths = {file.toString()};
        String[] mimeTypes = {"/image/png"};
        MediaScannerConnection.scanFile(MyClass.this, paths, mimeTypes, null);
        Toast.makeText(MyClass.this, "Image Saved", Toast.LENGTH_LONG).show();

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

这是意图额外流的代码,它采用我的位图URI并正确发送图像,保存方法也在那里。保存为5.8kb 960 * 960并发送为15kb,960 * 960。我希望时间都相同。

2 个答案:

答案 0 :(得分:0)

在saveImage(位图位图)方法中,您使用了&#34; bitmap.compress(Bitmap.CompressFormat.PNG,100,out)&#34;

根据您在compress方法中使用的参数,图像的大小会有所不同。图像质量可以设置为0(低)到100(高)。如果更新质量,则可以保持图像的大小。

答案 1 :(得分:0)

以前的答案是对的。要检查它,只需在发送图像之前使用方法saveImage()。

FileOutputStream out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush();
    out.close();
    String[] paths = {file.toString()};
    String[] mimeTypes = {"/image/png"};
    MediaScannerConnection.scanFile(MyClass.this, paths, mimeTypes, null);
    Toast.makeText(MyClass.this, "Image Saved", Toast.LENGTH_LONG).show();

<强>更新

Some understanding files size in different systems and OS.