android - Android 中的图像路径

标签 android image andengine

我在我的 Android 应用程序中使用 andengine 并希望显示设备图库中的图像。如何获取图像路径? 请注意,我不想使用这样的东西:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);

我需要的只是图像的路径。你能帮帮我吗?

最佳答案

请看下面的代码,

public class select_Gallery_image extends Activity{

    private static final int SELECT_PICTURE = 1;
    protected String _path;
    protected boolean _taken;   
    protected static final String PHOTO_TAKEN   = "photo_taken";
    ProgressDialog PD1;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        //intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, SELECT_PICTURE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (resultCode == RESULT_OK)
        {
            if (requestCode == SELECT_PICTURE) 
            {
                settings.bitmap=null;
                Uri selectedImageUri = data.getData();
                settings.selectedImagePath = getPath(selectedImageUri);
                File filenew = new File(settings.selectedImagePath);
                int file_size = Integer.parseInt(String.valueOf(filenew.length()/1024));
                if(file_size<= 10000){
                PD1 = ProgressDialog.show(select_Gallery_image.this,"","Loading");
                new Thread(new Runnable() {

                    public void run() {
                        Looper.prepare();
                        settings.bitmap = decodeFile(settings.selectedImagePath);
                        imagehandler.sendEmptyMessage(0);

                    }
                }).start();
                }
                else{
                    AlertDialog.Builder alertbox = new AlertDialog.Builder(select_Gallery_image.this);
                    alertbox.setMessage("Take Image Size Less than 10 MB");
                    alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        finish();
                         }
                    });
                    alertbox.show();
                }
             }
        }
    }
    private Handler imagehandler = new Handler(){
          public void handleMessage(Message msg){
             PD1.dismiss();
             settings.img_logo.setImageBitmap(settings.bitmap); 
             settings.img_logo.setBackgroundColor(Color.DKGRAY);
              finish(); 
          }
    };


    public String getPath(Uri uri) 
    {
        String[] projection = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(uri, projection, null, null, null);
      int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);

    }

     static Bitmap decodeFile(String str){
            try {
                //decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(str),null,o);

                //Find the correct scale value. It should be the power of 2.
                final int REQUIRED_SIZE=70;
                int width_tmp=o.outWidth, height_tmp=o.outHeight;
                int scale=1;
                while(true){
                    if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                        break;
                    width_tmp/=2;
                    height_tmp/=2;
                    scale++;
                }

                //decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize=scale;
                return BitmapFactory.decodeStream(new FileInputStream(str), null, o2);
            } catch (FileNotFoundException e) {}
            return null;
        }

     @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if(keyCode == KeyEvent.KEYCODE_BACK){
             finish();
         }
        return super.onKeyDown(keyCode, event);
    }
}

关于android - Android 中的图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9748278/

相关文章:

Android 从 Intent 中捕获数据

java - 如何更改 Sprite 随机出现在场景中的位置?

java - 如何根据当前位置获取最近的地点(用户输入的)?

android - 无法解析类 android.support.constraint.ConstraintLayout

javascript - 使用 javascript 和 jquery 将图像添加到 svg 文件

JavaScript 通过 Z-index 在图像之间进行转换

image - 使用验证器设置图像最大文件大小

java - Tiled Editor 贴图优化

android - AndEngine GlES2.0 使文本闪烁

android - 移动设备上出现 404 错误,但在 PC 浏览器上有效