如何将图像从Android库加载到ImageView?

时间:2018-01-21 14:52:32

标签: java android image

我正在尝试将Android设备中的图像加载到我的应用程序中。

我已经在我的代码中使用了执行时间的权限,因为我发现它在最近的Api中是必要的,但它的工作方式不同。

我可以单击按钮并选择图像,但它不会出现在我的ImageView中。

这是我的ActivityCode:

package geoapp.testemapas.com.geoapp;

import android.app.VoiceInteractor;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;

public class PegarImagemActivity extends AppCompatActivity {

public static final int IMAGEM_ID = 1;
public static final int PERMISSAO_REQUEST = 2;

private Button pegarImgemBotaoPegarImagem;
private Button PegarImgemBotaoEnviarImagem;
private ImageView pegarImagemImageViewImgem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pegar_imagem);

    pegarImgemBotaoPegarImagem = (Button) findViewById(R.id.pegar_imagem_botao_pegar_imagem);
    pegarImagemImageViewImgem = (ImageView) findViewById(R.id.pegar_imagem_iv);

    pegarImgemBotaoPegarImagem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, IMAGEM_ID);
        }
    });

    if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)){

        }else{
            ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSAO_REQUEST );
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if(requestCode == IMAGEM_ID){
        if(requestCode == RESULT_OK){

            Uri imagem_selecionada = intent.getData();
            String[] colunas =  {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(imagem_selecionada, colunas, null, null, null);
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(colunas[0]);
            String pathImg = cursor.getString(index);
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(pathImg);
            pegarImagemImageViewImgem.setImageBitmap(bitmap);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSAO_REQUEST){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

        }else{

        }
        return;
    }
}
}

2 个答案:

答案 0 :(得分:0)

你可以尝试这种方法。

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
1001);

在活动结果中,您将获得所选图像,并可在图像视图中显示

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == 1001) {
        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();

        bitmap = BitmapFactory.decodeFile(picturePath);

        if (bitmap != null) {
            ImageView img = (ImageView)findViewById(R.id.imgeview_id);
            img.setImageBitmap(bitmap);
        }
    }
 }

答案 1 :(得分:0)

看起来不错!需要进行一些修正才能使其适合您。

  1. 在Manifest文件中添加使用权限。

  2. 正确的代码错误。

    if(resultCode == RESULT_OK){

    }

  3. 那就是:)