2

我有imageview一个默认图像,我希望当用户单击 imageview 打开图像选择器然后他从 sdcard 中选择他的图像然后图像在 imageview 上,

这是我的图像视图

xml

<ImageView
            android:id="@+id/ivImage"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginLeft="10dip"
            android:contentDescription="@string/iv_undefinedImage"
            android:src="@drawable/undefinedimage" />

爪哇

ImageView iv ;
iv_image = (ImageView)findViewById(R.id.iv_signup_image);
        iv_image.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
        case R.id.iv_signup_image:
break;
}
4

4 回答 4

3

我想这就是你要找的

if (Environment.getExternalStorageState().equals("mounted")) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);
    startActivityForResult(
        Intent.createChooser(
            intent,
            "Select Picture:"),
        requestCode);
}

并处理回调

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImageUri = data.getData();
    String selectedImagePath = getPath(selectedImageUri);
    Bitmap photo = getPreview(selectedImagePath);
}


public String getPath(Uri uri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null, null);
    if(cursor.moveToFirst()){;
        int column_index = cursor.getColumnIndexOrThrow(proj[0]);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

public Bitmap getPreview(String fileName) {
    File image = new File(fileName);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);
    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
        return null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
        : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / 64;
    return BitmapFactory.decodeFile(image.getPath(), opts);
}

希望能帮助到你

于 2013-02-07T06:31:37.903 回答
1

您需要将图像从 SDCard 加载到位图,然后将位图图像设置为 ImageView:

Bitmap bmp = BitmapFactory.decodeFile("/path/to/file.png");
iv_image.setImageBitmap(bmp);

如前所述yahya,您还可以从 SDCard 图像文件中创建一个可绘制对象,然后设置该图像可绘制对象:

iv_image.setImageDrawable(Drawable.createFromPath("/path/to/file.png"));

您还应该确保在您的清单中包含对 SDCard 的读取(/写入)权限。

于 2013-02-07T06:22:31.700 回答
1

试试下面的代码。

public class MainActivity extends Activity {
ImageView iv_image,img1;
int column_index;
  Intent intent=null;
// Declare our Views, so we can access them later
String logo,imagePath,Logo;
Cursor cursor;
//YOU CAN EDIT THIS TO WHATEVER YOU WANT
private static final int SELECT_PICTURE = 1;

 String selectedImagePath;
//ADDED
 String filemanagerstring;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    img1= (ImageView)findViewById(R.id.image1);
    iv_image= (ImageView)findViewById(R.id.iv_signup_image);

    img1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            // in onCreate or any event where your want the user to
            // select a file
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
       }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();

            //OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);


            img.setImageURI(selectedImageUri);

           imagePath.getBytes();
           TextView txt = (TextView)findViewById(R.id.title);
           txt.setText(imagePath.toString());

           Bitmap bm = BitmapFactory.decodeFile(imagePath);
           iv_image.setImageBitmap(bm);



        }

    }

}

//UPDATED!
public String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor
        .getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
 imagePath = cursor.getString(column_index);

return cursor.getString(column_index);
}

}

我希望它会帮助你。

谢谢。

于 2013-02-07T06:33:18.243 回答
1
private final int GET_USER_IMAGE_FROM_GALLERY = 10;

ImageView imageView = findViewById(R.id.ivImage);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
        "Select Picture"),
GET_USER_IMAGE_FROM_GALLERY);
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == GET_USER_IMAGE_FROM_GALLERY) {

        if (data != null) {

            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);

            try {
                File imageFile = new File(selectedImagePath);
                Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                        .getAbsolutePath());

                imageView.setImageBitmap(bitmap);
            } catch (Exception e) {
            }

        }
}

private String getPath(Uri selectedImageUri) {

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(selectedImageUri,
            projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
于 2013-02-07T06:36:06.240 回答