访问和使用图库中的图像
Accessing and using images from the Gallery
我花了几天时间研究这个问题,但我一无所获。别人认为正确的东西对我根本不起作用,而且他们使用的关键字通常甚至不存在。
我想做的就是让用户 select 来自画廊的图片。我想将这个 selection 存储为位图,转换图像的大小,然后将其保存到文件。
他们可以选择使用摄像头做同样的事情,而且这段代码工作得很好。不知道为什么画廊这么难。这是我的代码(从下面的行开始,由于某种原因不会添加到代码块中):
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && null != data) {
final Context context=this;
Intent j=new Intent(context,PlayerSelection.class);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri selectedImageUri = data.getData();
String selectedImage = selectedImageUri.getPath();
Toast.makeText(getApplicationContext(), selectedImage, Toast.LENGTH_LONG).show();
String fName = new File(getFilesDir(), selectedImage).getAbsolutePath();
fName = fName + ".jpg";
Toast.makeText(getApplicationContext(), fName, Toast.LENGTH_LONG).show();
try {
Bitmap galleryTemp = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImageUri);
} catch (IOException e) {
e.printStackTrace();
}
//Bitmap galleryPic = Bitmap.createScaledBitmap(galleryTemp, 50, 50, false);
//saveImageFile(galleryTemp);
//startActivity(j);
}
我在上面注释掉的部分是应用程序失败的地方。当我得到文件的路径时,我被定向到 data\data\app.name\files\external\images\media630,
但实际文件存在于 **Phone\DCIM\Camera**
我认为这就是问题所在,因为当我尝试对图像 (Bitmap galleryTemp) 进行操作或执行任何操作时,应用程序崩溃了。
这是我调用上述过程的方法:
案例R.id.btnGallery:
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_REQUEST);
break;
有什么想法吗?我几乎阅读了关于这个主题的所有内容,但没有任何内容对我有用。
这可能对您有帮助:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
您必须从 'Media Store' 内容提供者那里获取路径,该内容提供者会为您提供实际图像、其缩略图等的路径。
由于权限问题,代码失败。对于遇到同样问题的任何其他人,请尝试将此权限行添加到您的 manifest.xml 文件中:
<xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.appname" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".Globals"
android:theme="@style/AppTheme" >
我花了几天时间研究这个问题,但我一无所获。别人认为正确的东西对我根本不起作用,而且他们使用的关键字通常甚至不存在。
我想做的就是让用户 select 来自画廊的图片。我想将这个 selection 存储为位图,转换图像的大小,然后将其保存到文件。
他们可以选择使用摄像头做同样的事情,而且这段代码工作得很好。不知道为什么画廊这么难。这是我的代码(从下面的行开始,由于某种原因不会添加到代码块中):
if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && null != data) {
final Context context=this;
Intent j=new Intent(context,PlayerSelection.class);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri selectedImageUri = data.getData();
String selectedImage = selectedImageUri.getPath();
Toast.makeText(getApplicationContext(), selectedImage, Toast.LENGTH_LONG).show();
String fName = new File(getFilesDir(), selectedImage).getAbsolutePath();
fName = fName + ".jpg";
Toast.makeText(getApplicationContext(), fName, Toast.LENGTH_LONG).show();
try {
Bitmap galleryTemp = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImageUri);
} catch (IOException e) {
e.printStackTrace();
}
//Bitmap galleryPic = Bitmap.createScaledBitmap(galleryTemp, 50, 50, false);
//saveImageFile(galleryTemp);
//startActivity(j);
}
我在上面注释掉的部分是应用程序失败的地方。当我得到文件的路径时,我被定向到 data\data\app.name\files\external\images\media630, 但实际文件存在于 **Phone\DCIM\Camera**
我认为这就是问题所在,因为当我尝试对图像 (Bitmap galleryTemp) 进行操作或执行任何操作时,应用程序崩溃了。
这是我调用上述过程的方法:
案例R.id.btnGallery:
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_REQUEST);
break;
有什么想法吗?我几乎阅读了关于这个主题的所有内容,但没有任何内容对我有用。
这可能对您有帮助:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
您必须从 'Media Store' 内容提供者那里获取路径,该内容提供者会为您提供实际图像、其缩略图等的路径。
由于权限问题,代码失败。对于遇到同样问题的任何其他人,请尝试将此权限行添加到您的 manifest.xml 文件中:
<xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.appname" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".Globals"
android:theme="@style/AppTheme" >