如何把我自己的图像而不是mediastore.images.media.data?

时间:2014-07-15 07:54:27

标签: android

如何从目录中放置图像而不是从媒体加载。我从这样的画廊加载 String [] filePathColumn = {MediaStore.Images.Media.DATA};。如何从/ assets或/ drawable-mdpi加载?这是我的代码。请告诉我如何使用此代码从我的目录中添加图像?

public class ImageGalleryDemoActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        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();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
}

1 个答案:

答案 0 :(得分:0)

您可以将各个drawables放在android drawables文件夹中,Android会根据设备显示密度自动选择所需的drawable。您可以将drawables设置为

imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image));

您也可以从here获取帮助。