我从图库中的位图中保存了一些图像,它工作正常,但是它保存了一些随机名称,我不知道如何更改图像的名称在保存之前。我怎样才能做到这一点?这是我的代码:
public void save(View v) throws UnsupportedEncodingException {
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);
tempDir = "android.resource://" + getPackageName() + "/";
cw = new ContextWrapper(getApplicationContext());
directory = cw.getDir(getPackageName(), Context.MODE_PRIVATE);
prepareDirectory();
current = osID + ".png";
mypath = new File(directory, current);
Canvas canvas = new Canvas(mBitmap);
try {
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
v.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
byte[] bytes = getByteArray(mBitmap);
mFileOutStream.flush();
mFileOutStream.close();
String pathPhotoSelected = IOUtil.getRealPathFromURI(CaptureSignature.this, getImageUri(CaptureSignature.this, mBitmap));
Log.d("PATH", pathPhotoSelected);
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, osID, null);
return Uri.parse(path);
}
public static String getRealPathFromURI(Activity activity, Uri contentUri) {
try {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
int indexData = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(indexData);
} catch (Exception e) {
return contentUri.getPath();
}
}
private boolean prepareDirectory() {
try {
if (makedirs()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", 1000).show();
return false;
}
}