首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android pick图片来自图库

Android pick图片来自图库
EN

Stack Overflow用户
提问于 2016-12-16 13:44:50
回答 3查看 389关注 0票数 2

我已经从图库中选择了一张图片,现在我想要的是当用户重新打开应用程序时,图片在ImageView中,请给我一些建议,这是我的代码

代码语言:javascript
复制
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == RESULT_LOAD_IMG && 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]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.profileimg);
                SharedPreferences.Editor editor = getSharedPreferences(AppConstants.VERIFICATION, MODE_PRIVATE).edit();
                editor.putString(AppConstants.PROFILEIMAGE, imgDecodableString);
                editor.commit();
            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
EN

回答 3

Stack Overflow用户

发布于 2016-12-16 13:49:36

尝试以下代码:

代码语言:javascript
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == RESULT_LOAD_IMG && 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]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();

            imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.profileimg);
            Bitmap bmap = imgView.getDrawingCache();    
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);
            byte[]imagebytes=bytes.toByteArray();
            String encodedImage = Base64.encodeToString(imagebytes, Base64.DEFAULT);
            SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(ProfilePage.this);
            SharedPreferences.Editor edit=shre.edit();
            edit.putString("image_data",encodedImage);
            edit.commit();
        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

将此代码添加到下面的代码以获取共享首选项

代码语言:javascript
复制
 SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
    String previouslyEncodedImage = shre.getString("image_data", "");
    if( !previouslyEncodedImage.equalsIgnoreCase("") ){
        byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
        imgView.setImageBitmap(bitmap);
    }
票数 1
EN

Stack Overflow用户

发布于 2016-12-16 13:53:38

使用

代码语言:javascript
复制
    @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                try {
                    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                            && null != data) {
                        InputStream inputStream = getContentResolver()
                                .openInputStream(data.getData());
                    File outFile = new File(getCacheDir(),"tempImage.png");

                   FileOutputStream fileOutputStream = new FileOutputStream(
                            outFile);
                    copyStream(inputStream, fileOutputStream);
                    fileOutputStream.close();
                    inputStream.close();
                        Bitmap bm = BitmapFactory.decodeStream(inputStream);

                        imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.profileimg);
                        imgView.setImageBitmap(bm);
                        SharedPreferences.Editor editor = getSharedPreferences(AppConstants.VERIFICATION, MODE_PRIVATE).edit();
                        editor.putString(AppConstants.PROFILEIMAGE, outFile.getAbsolutePath());
                        editor.commit();
                    } else {
                        Toast.makeText(this, "You haven't picked Image",
                                Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                            .show();
                }

public static void copyStream(InputStream input, OutputStream output)
            throws IOException {

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2016-12-16 13:55:15

代码语言:javascript
复制
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

         // Do something with the bitmap


        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41177962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档