在android上浏览SD卡按钮单击
我有下面的代码可以在点击按钮时浏览图库在android上浏览SD卡按钮单击,android,android-intent,android-sdcard,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); st
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新手。我怎么做?请帮帮我。尝试使用动作媒体 请参阅有关的详细信息,您不能使用此功能
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" />
那么问题是什么呢?您的代码显示您已在单击按钮时实现。其显示库。相反,我需要存储SD卡。您想打开一个特定的位置还是只从父目录中浏览?dir@PiyushGupta认为他需要打开一个位置或浏览SD卡而不是gallery。@user2450263是的。@user3377887然后使用GetExternalDirectority函数从文件中获取图像。单击后,我应该能够浏览SD卡的位置
// 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;
}