Android保存视图到jpg或png

sha*_*ess 39 camera android image

我想写一个Android应用程序,基本上在另一个图像上的图像上叠加,然后我想保存图片与叠加作为jpg或png.基本上这将是我想要保存的整个视图.

示例代码非常有用.

编辑:

我尝试了你的建议,并在Starred Line获得一个空指针.

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class EditPhoto extends Activity {
        /** Called when the activity is first created. */
     LinearLayout ll = null;
     TextView tv = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.text);
            ll = (LinearLayout) findViewById(R.id.layout);
            ll.setDrawingCacheEnabled(true);
            Bitmap b = ll.getDrawingCache();
            File sdCard = Environment.getExternalStorageDirectory();
            File file = new File(sdCard, "image.jpg");
            FileOutputStream fos;
      try {
       fos = new FileOutputStream(file);
       *** b.compress(CompressFormat.JPEG, 95,fos);
      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

        }
    }
Run Code Online (Sandbox Code Playgroud)

Mon*_*der 83

您可以利用View的绘图缓存.

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));
Run Code Online (Sandbox Code Playgroud)

视图是您的视图.95是JPG压缩的质量.而文件输出流就是这样.

  • 它从手机根开始.因此,如果要从SD卡加载某些内容,请使用Environment.getExternalStorageDirectory()来获取sdcard的根目录. (2认同)
  • 看起来它的确有效,感谢你几乎所有的代表:).多谢你们!! (2认同)

plu*_*ind 6

File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos = new FileOutputStream(file);
Run Code Online (Sandbox Code Playgroud)

在Moncader的答案中使用fos引用作为b.compress()方法的第3个参数.图像将作为image.jpg存储在SD卡的根目录中.


Har*_*esh 5

将 RelativeLayout 或任何视图保存到图像 (.jpg)

String getSaveImageFilePath() {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), YOUR_FOLDER_NAME);
    // Create a storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(YOUR_FOLDER_NAME, "Failed to create directory");
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageName = "IMG_" + timeStamp + ".jpg";

    String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
    Log.d(YOUR_FOLDER_NAME, "selected camera path " + selectedOutputPath);

    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());

    int maxSize = 1080;

    int bWidth = bitmap.getWidth();
    int bHeight = bitmap.getHeight();

    if (bWidth > bHeight) {
        int imageHeight = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
        bitmap = Bitmap.createScaledBitmap(bitmap, maxSize, imageHeight, true);
    } else {
        int imageWidth = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
        bitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, maxSize, true);
    }
    view.setDrawingCacheEnabled(false);
    view.destroyDrawingCache();

    OutputStream fOut = null;
    try {
        File file = new File(selectedOutputPath);
        fOut = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return selectedOutputPath;
}
Run Code Online (Sandbox Code Playgroud)