我会把这个作为一条评论,因为我没有时间详细解释,但是它明显太长了。尝试像这样做(我用这个将位图存储到图库中)。注意,您似乎只是在扫描图像。目前,您似乎正在将文件存储到内部存储而不是手机内容(图库)。
假设我们从ActivityA.class运行此代码,该类扩展自Activity
public class SaveBitmapToDevice extends AsyncTask<Bitmap, Void, String>{
private final ShareType type;
public SaveBitmapToDevice(ShareType type){
this.type = type;
}
@Override
protected String doInBackground(Bitmap... cards) {
return insertImageIntoGallery(getContentResolver(), cards[0], getString(R.string.card_gallery_title),
getString(R.string.card_gallery_label));
}
public String insertImageIntoGallery(ContentResolver cr, Bitmap source, String title, String description) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, title);
values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
values.put(MediaStore.Images.Media.DESCRIPTION, description);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
Uri url = null;
String stringUrl = null;
try {
url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (source != null) {
OutputStream imageOut = cr.openOutputStream(url);
try {
source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
} finally {
imageOut.close();
}
long id = ContentUris.parseId(url);
Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null);
storeThumbnail(cr, miniThumb, id, 50F, 50F, MediaStore.Images.Thumbnails.MICRO_KIND);
} else {
cr.delete(url, null, null);
return storeToAlternateSd(source, title);
}
} catch (Exception e) {
if (url != null) {
cr.delete(url, null, null);
return storeToAlternateSd(source, title);
}
}
savedOnSD = false;
if (url != null) {
stringUrl = url.toString();
}
return stringUrl;
}
private Bitmap storeThumbnail(
ContentResolver cr,
Bitmap source,
long id,
float width,
float height,
int kind) {
Matrix matrix = new Matrix();
float scaleX = width / source.getWidth();
float scaleY = height / source.getHeight();
matrix.setScale(scaleX, scaleY);
Bitmap thumb = Bitmap.createBitmap(source, 0, 0,
source.getWidth(),
source.getHeight(), matrix,
true
);
ContentValues values = new ContentValues(4);
values.put(MediaStore.Images.Thumbnails.KIND,kind);
values.put(MediaStore.Images.Thumbnails.IMAGE_ID,(int)id);
values.put(MediaStore.Images.Thumbnails.HEIGHT,thumb.getHeight());
values.put(MediaStore.Images.Thumbnails.WIDTH,thumb.getWidth());
Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);
try {
OutputStream thumbOut = cr.openOutputStream(url);
thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
thumbOut.close();
return thumb;
} catch (FileNotFoundException ex) {
Log.e("IMAGE_COMPRESSION_ERROR", "File not found");
ex.printStackTrace();
return null;
} catch (IOException ex) {
Log.e("IMAGE_COMPRESSION_ERROR", "IO Exception");
ex.printStackTrace();
return null;
}
}
private String storeToAlternateSd(Bitmap src, String title){
if(src == null)
return null;
File sdCardDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "My Cards");
if(!sdCardDirectory.exists())
sdCardDirectory.mkdir();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy - (hh.mm.a)", Locale.US);
File image = new File(sdCardDirectory, title + " -- [" + sdf.format(new Date()) + "].jpg");
try {
FileOutputStream imageOut = new FileOutputStream(image);
src.compress(Bitmap.CompressFormat.JPEG, 100, imageOut);
imageOut.close();
savedOnSD = true;
return image.getAbsolutePath();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return null;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
@Override
public void onPostExecute(String url){
if(url != null){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if(savedOnSD){
File file = new File(url);
if(file.exists())
intent.setDataAndType(Uri.fromFile(file), "image/jpeg");
else
return;
}
else
intent.setDataAndType(Uri.parse(url), "image/jpeg");
ActivityA.this.startActivity(intent);
}
else
Toast.makeText(ActivityA.this, getString(R.string.error_compressing), Toast.LENGTH_SHORT).show();
}
}
getExternalFilesDir
,会怎样呢? - iCantC