我想将imageView保存到SD卡中但是当我尝试从imageView获取位图时,我得到以下异常(有时并不总是)。有人能帮帮我吗?提前致谢
Caused by: java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:922)
public static class SaveImageToSD extends AsyncTask<String, Void, String> {
Context context;
ImageView mImageView;
ProgressDialog progressDialog;
public SaveImageToSD(Context context, ImageView iv, String name) {
this.context = context;
this.mImageView = iv;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "", context.getResources().getString(R.string.please_wait), true);
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected String doInBackground(String... x) {
File projectFolder = new File(Environment.getExternalStorageDirectory() + File.separator + Settings.projectFolder + File.separator);
boolean folderCreateSuccess = true;
if (!projectFolder.exists()) {
folderCreateSuccess = projectFolder.mkdir();
}
if (folderCreateSuccess) {
Bitmap bitmap;
// Exception in if statement
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
File image = new File(projectFolder, "GE_" + System.currentTimeMillis() + ".jpg");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "title");
values.put(MediaStore.Images.Media.DESCRIPTION, "description");
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.ImageColumns.BUCKET_ID, image.toString().toLowerCase(Locale.US).hashCode());
values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, image.getName().toLowerCase(Locale.US));
values.put("_data", image.getAbsolutePath());
ContentResolver cr = context.getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
progressDialog.dismiss();
if (success) {
((ActionBarActivity)context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, context.getResources().getString(R.string.image_successfully_saved), Toast.LENGTH_SHORT).show();
}
});
} else {
((ActionBarActivity)context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, context.getResources().getString(R.string.image_successfully_saved), Toast.LENGTH_SHORT).show();
}
});
}
} else {
Log.i("Create Folder", "Error during create folder");
}
return "";
}
}
设置图像我使用以下代码,我使用transparentDrawable因为Picasso包装内容问题
transparentDrawable.setBounds(new Rect(0, 0, 1000, 1000));
Picasso.with(mContext).load(((FBPhotoCard) mImageCards.get(position)).getThumbnail()).placeholder(transparentDrawable).noFade().into(holder.imageView);
答案 0 :(得分:0)
我认为异常是因为行d.getIntrinsicWidth(), d.getIntrinsicHeight()
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
根据Android docs drawable.getIntrinsicWidth()
返回底层可绘制对象的固有宽度。如果没有固有宽度,则返回-1,例如使用纯色。因此,请确保将大于1的值传递给Bitmap.createBitmap()
方法
答案 1 :(得分:0)
你得到这个错误,因为d.getIntrinsicWidth(), d.getIntrinsicHeight()
返回0尝试放置一些常量值,然后像100一样检查。