在Android按钮上单击浏览SD卡

时间:2014-03-04 07:02:17

标签: android android-intent android-sdcard

我有以下用于在按钮点击上浏览图库的代码。

loadFile.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,RESULT_LOAD_IMAGE);
   }
});

相反,我需要在按钮点击时访问SD卡。 我已将代码编辑为:

Intent i = new Intent(Environment.getExternalStorageDirectory().getAbsolutePath());

我收到了错误。我是android的新手。我该怎么做?请帮帮我。

3 个答案:

答案 0 :(得分:0)

你不能用它。

Intent i = new Intent(Environment.getExternalStorageDirectory().getAbsolutePath());

你应该改变这个

String myFilepath = Environment.getExternalStorageDirectory().getAbsolutePath();

File f = new File(myFilepath);

添加清单文件的权限。

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

// try this way
 final private int PICK_IMAGE = 1;
 private String selectedImage;

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PICK_IMAGE) {
                selectedImage = getAbsolutePath(data.getData());
                yourImageView.setImageBitmap(decodeFile(selectedImage));
            }  else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

}

public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
}

public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

}

答案 2 :(得分:-1)