我尝试创建一个应用程序,在其中一个活动上显示设备上的所有图像,供用户选择其中一个。
我设法只显示DCIM / Camera文件夹中的所有图像。
这是我显示图片的代码:
GridView gridview = (GridView) findViewById(R.id.gridview);
imageAdapter = new ImageAdapter(this);
gridview.setAdapter(imageAdapter);
gridview.setBackgroundColor(getResources().getColor(R.color.Black));
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files)
imageAdapter.add(file.getAbsolutePath());
有人可以帮助我吗?
提前谢谢!
答案 0 :(得分:0)
点击(可能是上传按钮):
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
当用户选择图像时:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 8;
final InputStream ist;
try {
ist = mActivity.getContentResolver().openInputStream(selectedImage);
Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
mPicturePath = compressImage(bitmap).getAbsolutePath();
//recycling bitMap to overcome OutOfMemoryError
if(bitmap!=null){
bitmap.recycle();
bitmap=null;
}
ist.close();
//you have mPicturePath do something with it!
} catch (FileNotFoundException e) {
logoUploadFaied();
e.printStackTrace();
} catch (IOException e) {
logoUploadFaied();
e.printStackTrace();
}
} else {
//Not uploading
}
}
这是压缩图像代码,它接受位图作为参数,将其存储在本地(压缩)并返回文件路径。如果您想将图像上传到服务器,例如whatsapp,那就太棒了:
public File compressImage(Bitmap bmp) {
Bitmap scaledBitmap = null;
int actualHeight = bmp.getHeight();
int actualWidth = bmp.getWidth();
// max Height and width values of the compressed image is taken as 816x612
float imgRatio = actualWidth / actualHeight;
float maxRatio = logoMaxWidth / logoMaxHeight;
// width and height values are set maintaining the aspect ratio of the image
if (actualHeight > logoMaxHeight || actualWidth > logoMaxWidth) {
if (imgRatio < maxRatio) {
imgRatio = logoMaxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) logoMaxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = logoMaxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) logoMaxWidth;
} else {
actualHeight = (int) logoMaxHeight;
actualWidth = (int) logoMaxWidth;
}
}
try {
scaledBitmap = Bitmap.createScaledBitmap(bmp, actualWidth, actualHeight, true);
} catch (OutOfMemoryError exception) {
logoUploadFaied();
exception.printStackTrace();
}
String uriSting = (System.currentTimeMillis() + ".jpg");
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + uriSting);
try {
file.createNewFile();
} catch (IOException e) {
logoUploadFaied();
e.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
logoUploadFaied();
e.printStackTrace();
}
try {
fos.write(getBytesFromBitmap(scaledBitmap));
fos.close();
//recycling bitMap to overcome OutOfMemoryError
if(scaledBitmap!=null){
scaledBitmap.recycle();
scaledBitmap=null;
}
} catch (IOException e) {
logoUploadFaied();
e.printStackTrace();
}
return file;
}