我搜索了很多天how to get image from gallery
我承认我发现了很多代码,但没有人工作。
我现在有这个代码,当我点击按钮并选择一个图像时程序停止"不幸的是应用已停止" 。
任何帮助将不胜感激 ...
这是代码
public class MainActivity extends AppCompatActivity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLoadImage = (Button) findViewById(R.id.button);
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.ImageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
答案 0 :(得分:0)
以下是从库中选择图片或从相机中捕捉图片并在图片视图中使用图片的工作解决方案。
来源:SO
首先在onCreate
方法中初始化imageview。然后通过任何按钮的selectimage
事件调用onClick
函数,就是这样。
//functions to select image from the device
private void selectImage() {
final CharSequence[] items = {"Take Photo","Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(signature_new.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageF.setImage(ImageSource.bitmap(thumbnail));
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
try {
Bitmap bm=decodeUri(selectedImageUri);
imageViewF.setImage(ImageSource.bitmap(bm));
//uploadbm=bm;
//dialog_dimension();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
}