我试图通过从图库中选择一个图像到android中的ImageView。我正在使用意图打开库和OnActivityResult来处理它。问题是OnActivityResult无法正常工作。画廊打开,我选择图像,然后它仍然是旧图像。没有错误。
以下是我打开图库的代码:
wallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
这就是我如何处理它:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
wallpaper.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();
// Even the Toast is not working.
}
}
我确信,ImageView被正确声明,因为当我使用资源作为壁纸时,它正在工作。知道问题来自哪里?
编辑: onActivityResult()正在调用,但IF中的代码未被访问/触发。
这是ImageView,我需要更改:
<HorizontalScrollView
android:id="@+id/wallScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center">
<ImageView
android:id="@+id/wallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/wall"
android:scaleType="centerCrop"/>
</HorizontalScrollView>
我发现这在某种程度上导致了问题的原因?
修改 ImageView声明:
在OnCreate()之前:
ImageView wallpaper;
在onCreate()中:
wallpaper = (ImageView)findViewById(R.id.wallpaper);
并且onClickListener已经在上面了。
我发现在库打开后立即调用onActivityResult()。它不会触发IF,因为它不会等我选择图像。知道为什么会这样吗?
解决:
问题来自于,当画廊被打开时,我的活动重新启动,因为OnActivityResult()被调用得太早了。这不会发生在所有设备上,但要确保它可以在所有设备上工作,只需添加到清单中:
android:launchMode="singleTop"
每个人,谢谢你的帮助!
答案 0 :(得分:2)
这是一个正在运行的示例Activity类...
package com.example.testproject;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
ImageView ivSelectedPhoto;
public static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivSelectedPhoto = (ImageView) findViewById(R.id.ivGalery) ;
Button btnOpenGalery = (Button) findViewById(R.id.btnSelectImage);
btnOpenGalery.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
}
});
}
@Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent Data) {
super.onActivityResult(RequestCode, ResultCode, Data);
if (RequestCode == RESULT_LOAD_IMAGE && ResultCode == RESULT_OK && null != Data) {
Uri SelectedImage = Data.getData();
String[] FilePathColumn = {MediaStore.Images.Media.DATA };
Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
SelectedCursor.moveToFirst();
int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
String picturePath = SelectedCursor.getString(columnIndex);
SelectedCursor.close();
ivSelectedPhoto.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();
}
}
}
我现在正在为你制作:)你不要忘记在 onActivityResult()方法上添加 @Override :)
答案 1 :(得分:0)
请试试这个:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
}
}
}
删除 null!=数据代码..
if(resultCode == RESULT_OK),数据不为空;)