Select 图片来自图库并在图片视图中显示
Select image from gallery and show in imageview
我需要在手机 phone 和 select 中打开我的画廊 activity 上的图像视图中打开的一张图片..没什么难的..但是我在模拟器中有代码和这个代码(genymotion) 运行 完美.. 但在真正的 phone 小米 Mi4 上什么都没有。
打开图库 select 项,但什么也没有。
我没有更多 phones :(
我试着下载一些这个主题的例子,每一个都是一样的.. 当我 select item app 什么都不做时,galery 打开。
你有一些项目使用图库中的 select 图片并在 imageview 中显示吗?如果是,请分享您的代码并将我上传到某处 .apk 进行尝试,因为我 .. :( :'(
我的gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "paradox.galopshop"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
})
//Add library
// loaderimage
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.google.firebase:firebase-storage:10.2.0'
compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.firebase:firebase-crash:10.2.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
谢谢。
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"),SELECT_PICTURE );
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode==RESULT_OK){
if(requestCode==SELECT_PICTURE){
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
Log.i("IMAGE PATH TAG", "Image Path : " + path);
// Set the image in ImageView
ImageView imageView=(ImageView)findViewById(R.id.imageView2);
imageView.setImageURI(selectedImageUri);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setText("Načítané");
}
}
}
}
private String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor!=null) {
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
} else {
Toast.makeText(this, "Cursor null" + proj, Toast.LENGTH_SHORT).show();
}
return res;
}
//////更新
protected void onImageViewClick(){
// ImageView imageView=(ImageView)findViewById(R.id.imageView2);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImagePicker imgpicker= new ImagePicker();
imgpicker.getPickImageIntent(getApplicationContext());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Bitmap bitmap = ImagePicker.getBitmapFromResult(this, resultCode, data);
if (null != bitmap && resultCode == RESULT_OK) {
ImageView imageView=(ImageView)findViewById(R.id.imageView2);
imageView.setImageBitmap(bitmap);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setText("Načítané");
}
//////////////////////编辑 2
我发现 firebase 崩溃时出现错误..
当我确认照片应用程序崩溃时..
arrow_drop_down
Exception java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { act=inline-data (has extras) }} to activity {paradox.galopshop/paradox.galopshop.All}: java.lang.NullPointerException: uri
android.app.ActivityThread.deliverResults (ActivityThread.java)
android.app.ActivityThread.handleSendResult (ActivityThread.java)
android.app.ActivityThread.access00 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage (ActivityThread.java)
android.os.Handler.dispatchMessage (Handler.java)
android.os.Looper.loop (Looper.java)
android.app.ActivityThread.main (ActivityThread.java)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java)
arrow_drop_down
Caused by java.lang.NullPointerException: uri
com.android.internal.util.Preconditions.checkNotNull (Preconditions.java)
android.content.ContentResolver.openAssetFileDescriptor (ContentResolver.java)
android.content.ContentResolver.openAssetFileDescriptor (ContentResolver.java)
paradox.galopshop.ImagePicker.decodeBitmap (ImagePicker.java:116)
paradox.galopshop.ImagePicker.getImageResized (ImagePicker.java:139)
paradox.galopshop.ImagePicker.getBitmapFromResult (ImagePicker.java:103)
paradox.galopshop.All.onActivityResult (All.java:363)
android.app.Activity.dispatchActivityResult (Activity.java)
android.app.ActivityThread.deliverResults (ActivityThread.java)
android.app.ActivityThread.handleSendResult (ActivityThread.java)
android.app.ActivityThread.access00 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage (ActivityThread.java)
android.os.Handler.dispatchMessage (Handler.java)
android.os.Looper.loop (Looper.java)
android.app.ActivityThread.main (ActivityThread.java)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java)
ImagePicker : 请在使用他们的库时向开发人员致谢
在 onActivityResult
内的 if 语句中,将 requestCode
更改为 requestCode & 0xffff
。
这里很多人没有注意到的是 requestCode
返回的是十六进制数而不是数字。
在您的 AndroidManifest.xml
中添加这些权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
使用此方法取图:
protected void onImageViewClick(){
// ImageView imageView=(ImageView)findViewById(R.id.imageView2);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage();
}
});
}
private void selectImage() {
Intent takeImageIntent = ImagePicker.getPickImageIntent(this);
if (takeImageIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takeImageIntent, REQUEST_IMAGE_CAPTURE);
}
}
然后用这个来接收它们:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = ImagePicker.getBitmapFromResult(this, resultCode, data);
if (null != bitmap && resultCode == RESULT_OK) {
//do what you want with the bitmap here
}
}
你试过了吗?
public void onActivityResult( ....) {
If ( resultCode == RESULT_OK) {
Uri selectedImg = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver, selectedImg);
If(bitmap != null) {
yourImageView.setImageBitmap(bitmap);
}
}
}
把这个代码放在里面(你用来去画廊的任何东西)
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
然后调用这个函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
Bitmap img=MediaStore.Images.Media.getBitmap(getContentResolver, filePath);
If(img!= null) {
image.setImageBitmap(img);
}
}
不要忘记调用此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我需要在手机 phone 和 select 中打开我的画廊 activity 上的图像视图中打开的一张图片..没什么难的..但是我在模拟器中有代码和这个代码(genymotion) 运行 完美.. 但在真正的 phone 小米 Mi4 上什么都没有。
打开图库 select 项,但什么也没有。
我没有更多 phones :(
我试着下载一些这个主题的例子,每一个都是一样的.. 当我 select item app 什么都不做时,galery 打开。
你有一些项目使用图库中的 select 图片并在 imageview 中显示吗?如果是,请分享您的代码并将我上传到某处 .apk 进行尝试,因为我 .. :( :'(
我的gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "paradox.galopshop"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
})
//Add library
// loaderimage
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.google.firebase:firebase-storage:10.2.0'
compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.firebase:firebase-crash:10.2.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
谢谢。
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"),SELECT_PICTURE );
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode==RESULT_OK){
if(requestCode==SELECT_PICTURE){
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
Log.i("IMAGE PATH TAG", "Image Path : " + path);
// Set the image in ImageView
ImageView imageView=(ImageView)findViewById(R.id.imageView2);
imageView.setImageURI(selectedImageUri);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setText("Načítané");
}
}
}
}
private String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor!=null) {
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
} else {
Toast.makeText(this, "Cursor null" + proj, Toast.LENGTH_SHORT).show();
}
return res;
}
//////更新
protected void onImageViewClick(){
// ImageView imageView=(ImageView)findViewById(R.id.imageView2);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImagePicker imgpicker= new ImagePicker();
imgpicker.getPickImageIntent(getApplicationContext());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Bitmap bitmap = ImagePicker.getBitmapFromResult(this, resultCode, data);
if (null != bitmap && resultCode == RESULT_OK) {
ImageView imageView=(ImageView)findViewById(R.id.imageView2);
imageView.setImageBitmap(bitmap);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setText("Načítané");
}
//////////////////////编辑 2
我发现 firebase 崩溃时出现错误..
当我确认照片应用程序崩溃时..
arrow_drop_down
Exception java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { act=inline-data (has extras) }} to activity {paradox.galopshop/paradox.galopshop.All}: java.lang.NullPointerException: uri
android.app.ActivityThread.deliverResults (ActivityThread.java)
android.app.ActivityThread.handleSendResult (ActivityThread.java)
android.app.ActivityThread.access00 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage (ActivityThread.java)
android.os.Handler.dispatchMessage (Handler.java)
android.os.Looper.loop (Looper.java)
android.app.ActivityThread.main (ActivityThread.java)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java)
arrow_drop_down
Caused by java.lang.NullPointerException: uri
com.android.internal.util.Preconditions.checkNotNull (Preconditions.java)
android.content.ContentResolver.openAssetFileDescriptor (ContentResolver.java)
android.content.ContentResolver.openAssetFileDescriptor (ContentResolver.java)
paradox.galopshop.ImagePicker.decodeBitmap (ImagePicker.java:116)
paradox.galopshop.ImagePicker.getImageResized (ImagePicker.java:139)
paradox.galopshop.ImagePicker.getBitmapFromResult (ImagePicker.java:103)
paradox.galopshop.All.onActivityResult (All.java:363)
android.app.Activity.dispatchActivityResult (Activity.java)
android.app.ActivityThread.deliverResults (ActivityThread.java)
android.app.ActivityThread.handleSendResult (ActivityThread.java)
android.app.ActivityThread.access00 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage (ActivityThread.java)
android.os.Handler.dispatchMessage (Handler.java)
android.os.Looper.loop (Looper.java)
android.app.ActivityThread.main (ActivityThread.java)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java)
ImagePicker : 请在使用他们的库时向开发人员致谢
在 onActivityResult
内的 if 语句中,将 requestCode
更改为 requestCode & 0xffff
。
这里很多人没有注意到的是 requestCode
返回的是十六进制数而不是数字。
在您的 AndroidManifest.xml
中添加这些权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
使用此方法取图:
protected void onImageViewClick(){
// ImageView imageView=(ImageView)findViewById(R.id.imageView2);
TextView tw=(TextView)findViewById(R.id.addimage);
tw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage();
}
});
}
private void selectImage() {
Intent takeImageIntent = ImagePicker.getPickImageIntent(this);
if (takeImageIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takeImageIntent, REQUEST_IMAGE_CAPTURE);
}
}
然后用这个来接收它们:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = ImagePicker.getBitmapFromResult(this, resultCode, data);
if (null != bitmap && resultCode == RESULT_OK) {
//do what you want with the bitmap here
}
}
你试过了吗?
public void onActivityResult( ....) {
If ( resultCode == RESULT_OK) {
Uri selectedImg = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver, selectedImg);
If(bitmap != null) {
yourImageView.setImageBitmap(bitmap);
}
}
}
把这个代码放在里面(你用来去画廊的任何东西)
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
然后调用这个函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
Bitmap img=MediaStore.Images.Media.getBitmap(getContentResolver, filePath);
If(img!= null) {
image.setImageBitmap(img);
}
}
不要忘记调用此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />