Java 如何以.jpg文件格式从库中获取图像?
我正试图从画廊中获取图像。这是给我的位图图像。我想在.jpg文件中的图像,以便我可以保存在我的数据库中的文件名 我遵循了本教程: 画廊图片选择代码:Java 如何以.jpg文件格式从库中获取图像?,java,android,file,bitmapimage,Java,Android,File,Bitmapimage,我正试图从画廊中获取图像。这是给我的位图图像。我想在.jpg文件中的图像,以便我可以保存在我的数据库中的文件名 我遵循了本教程: 画廊图片选择代码: @SuppressWarnings("deprecation") private void onSelectFromGalleryResult(Intent data) { Bitmap bm=null; if (data != null) { try { bm = MediaStore.
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
File file = new File(picturePath);// error line
mProfileImage = file;
profile_image.setImageBitmap(bm);
}
我试过这个。但我在文件上得到了空指针
例外情况:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
另外,我不希望这个新创建的文件保存在外部存储器中。这应该是一个临时文件。我该怎么做
谢谢你。好消息是,你比你想象的更接近成功
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
此时,如果bm!=null,您有一个位图对象。位图是Android的通用图像对象,可以使用了。它实际上可能已经是.jpg格式了,所以您只需将它写入一个文件。您希望将其写入一个临时文件,因此我将执行以下操作:
File outputDir = context.getCacheDir(); // Activity context
File outputFile = File.createTempFile("prefix", "extension", outputDir); // follow the API for createTempFile
无论如何,在这一点上,将位图
写入文件非常容易
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); //replace 100 with desired quality percentage.
byte[] byteArray = stream.toByteArray();
现在您有了一个字节数组。我将把它写进一个文件给你
如果要删除临时文件,请参阅此处以了解更多信息:
我希望这有帮助 看起来您的picturePath
为空。这就是您无法转换图像的原因。尝试添加此代码片段以获取所选图像的路径:
private String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
之后,您需要在SelectFromGalleryResult
上修改您的。删除/禁用lineString[]filePath={MediaStore.Images.Media.DATA}代码>等,并替换为以下内容
Uri selectedImageUri = Uri.parse(selectedImage);
String photoPath = getRealPathFromURI(selectedImageUri);
mProfileImage = new File(photoPath);
//check if you get something like this - file:///mnt/sdcard/yourselectedimage.png
Log.i("FilePath", mProfileImage.getAbsolutePath)
if(mProfileImage.isExist()){
//Check if the file is exist.
//Do something here (display the image using imageView/ convert the image into string)
}
问题:您需要将其转换为.jpg格式的原因是什么?可以是.gif、.png等吗?请检查我尝试了创建临时文件的答案。。但更进一步,我在转换文件时出错。你能检查一下编辑过的问题吗@Eric@Sid对不起,我想你误解我了。获得位图后,您希望将其转换为字节数组:“ByteArrayOutputStream=new ByteArrayOutputStream();bm.compress(位图.CompressFormat.JPEG,100,流)//用所需的质量百分比替换100。字节[]byteArray=stream.toByteArray();'转换为字节数组后,将该数组写入文件。在您的代码中,看起来您没有这样做。
Uri selectedImageUri = Uri.parse(selectedImage);
String photoPath = getRealPathFromURI(selectedImageUri);
mProfileImage = new File(photoPath);
//check if you get something like this - file:///mnt/sdcard/yourselectedimage.png
Log.i("FilePath", mProfileImage.getAbsolutePath)
if(mProfileImage.isExist()){
//Check if the file is exist.
//Do something here (display the image using imageView/ convert the image into string)
}