【问题标题】:handle the result form the gallery处理来自画廊的结果
【发布时间】:2016-09-07 14:07:05
【问题描述】:

我搜索了很多天关于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));
    }
}

}

【问题讨论】:

    标签: java android-studio imageview image-gallery


    【解决方案1】:

    以下是从库中选择图片或从相机捕获图片并在图像视图中使用的工作解决方案。

    来源:所以

    首先在onCreate方法中初始化imageview。然后通过任意按钮的onClick事件调用selectimage函数就可以了。

    //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();
                    }
    
                }
            }
        }
    

    【讨论】:

    • 非常感谢,我需要一些帮助,因为出了点问题,有些样本不正常>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2019-04-17
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多