我是Android,Eclipse和Java的新手。我仍在开发从相册中选择图像的应用程序,使用BitmapFactory将其存储在位图中,处理位图,并将处理后的值保存到新的图像文件中。 我想要的名称格式是nameoforiginalimage + _embed + .png
这是我的代码: 首先,我初始化我需要的变量:
public static final int PICK_IMAGE = 1;
private Bitmap sourceBitmap;
private String picturePath;
其次,我是通过意图调用相册:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_embed);
final Intent photoPickerIntent = new Intent (Intent.ACTION_PICK);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadImage);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg) {
photoPickerIntent.setType("image/*.png");
startActivityForResult(photoPickerIntent, PICK_IMAGE);
}
});
}
第三,我通过onActivityResult
将我选择的图像放入Bitmap@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri photoUri = data.getData();
switch(requestCode) {
case(PICK_IMAGE) :
if (resultCode == RESULT_OK) {
if (photoUri != null) {
try {
Cursor cursor = getContentResolver().query(photoUri, null, null, null, null);
cursor.moveToFirst();
int indexColumn = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
picturePath = cursor.getString(indexColumn);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = false;
opt.inScaled = false;
opt.inDensity = 0;
opt.inJustDecodeBounds = false;
opt.inPurgeable = false;
opt.inSampleSize = 1;
opt.inScreenDensity = 0;
opt.inTargetDensity = 0;
sourceBitmap = BitmapFactory.decodeFile(picturePath);
ImageView hostImageView = (ImageView) findViewById(R.id.viewHost);
hostImageView.setImageBitmap(sourceBitmap);
} catch (Exception e) {
e.printStackTrace();}
}
}
break;
}
最后,处理Bitmap,保存到新的图像文件,并在此代码中显示到imageView:
Button buttonProceed = (Button) findViewById(R.id.buttonProceed);
buttonProceed.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View arg) {
int maxHeight = sourceBitmap.getHeight();
int maxWidth = sourceBitmap.getWidth();
int density = sourceBitmap.getDensity();
int processArray[];
Bitmap onProcessBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
processArray = new int[maxHeight * maxWidth];
onProcessBitmap.getPixels(processArray, 0, maxWidth, 0, 0, maxWidth, maxHeight);
// the process going in there
for (int i = 0; i < processArray.length; i++) {
processArray[i] = processArray[i] / 2;
}
onProcessBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Bitmap.Config.ARGB_8888);
onProcessBitmap.setDensity(density);
int masterIndex = 0;
for (int j = 0; j < maxHeight; j++) {
for (int i = 0; i < maxWidth; i++) {
onProcessBitmap.setPixel(i, j, Color.argb(0xFF,
processArray[masterIndex] >> 16 & 0xFF,
processArray[masterIndex] >> 8 & 0xFF,
processArray[masterIndex++] & 0xFF));
}
}
//saving sequence going in there
Uri result = null;
String sdcardState = android.os.Environment.getExternalStorageState();
String destPath = null;
int indexSeparator = picturePath.lastIndexOf(File.separator);
int indexPoint = picturePath.lastIndexOf(".");
if (indexPoint < 1){
indexPoint = picturePath.length();
}
String fileNameDest = picturePath.substring(indexSeparator + 1, indexPoint);
fileNameDest += "_embed";
if (sdcardState.contentEquals(android.os.Environment.MEDIA_MOUNTED)) {
destPath = android.os.Environment.getExternalStorageDirectory() + File.separator + fileNameDest + ".png";
}
OutputStream fileOut = null;
try {
fileOut = new FileOutputStream(destPath);
onProcessBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
result = Uri.parse("file://" + destPath);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
fileOut.flush();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
ImageView resultImageView = (ImageView) findViewById(R.id.resultImageView);
resultImageView.setImageBitmap(onProcessBitmap);
}
});
}
到目前为止,我已成功将其保存到新的图像文件,但图像文件名保存为“_embed.png”,而不是“NameOfOriginalImage_embed.png”。
请有人帮助我,我的代码有什么问题?
(有关其他信息,如果它可以帮助解决问题。我开发这个 最低要求SDK = API 8 2.2 Froyo 目标SDK&amp; Compile = API 17 4.2 Jelly Bean)
问候!