java Android如何从sd卡动态查看图像?我的代码出错了
我能够在图像视图中放置图像。但当我动态地做的时候,我无法得到图像
让我们检查一下这段代码,看看有没有一个图像视图,我们可以得到其中的图像代码是
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img1;
String imageOne = "";
创建图像对象及其onclick方法
img1 = (ImageView) findViewById(R.id.imageView1);
((ImageView) findViewById(R.id.imageView1))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
从sd卡获取图像的方法
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);
img1.setImageURI(selectedImageUri);
BitmapDrawable drawable = (BitmapDrawable) img1.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] bb = bos.toByteArray();
imageOne = Base64.encodeToString(bb, Base64.DEFAULT);
imageOne = addQuotes(imageOne);
}
获取上面使用的Uri方法
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);
}
这段代码工作正常,但当我尝试动态转换它时,onActiveResult方法中出现了获取id错误
我的动态代码是
RelativeLayout layout = (RelativeLayout)findViewById(R.id.MultiQuesRelaLayOut);
final ImageView[] im=new ImageView[10];
for(int img=0;img<2.length;img++)
{
im[img]=new ImageView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin=25;
params.topMargin=(img+1)*(35+add);
im[img].setId(img);
im[img].layout(5,5,5,5);
im[img].setVisibility(View.VISIBLE);
im[img].setBackgroundColor(color.background_dark);
im[img].setPadding(5, 15, 5, 15);
im[img].setLayoutParams(params);
layout.addView(im[img]);
im[img].setOnClickListener(ImageOnClick(im[img]));
}
这里是动态图像视图的onclick方法
View.OnClickListener ImageOnClick(final ImageView image) {
return new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Image pressed is: "+ image.getId());
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
image.getId());
}
};
}
从sd卡获取图像的方法
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
data.setImageURI(selectedImageUri);
BitmapDrawable drawable = (BitmapDrawable) data.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] bb = bos.toByteArray();
}
}
Geturi方法对于这两种方法都是相同的
在onActivityResult
方法中,我无法获得图像视图的ID,这导致了问题,如何解决希望您的建议
谢谢
# 1 楼答案
加上
给你们班 而不是
将此方法添加到类中