无法将位图图像添加到图像视图

时间:2016-10-25 20:09:36

标签: java android android-studio android-camera android-imageview

图像保存在所需的文件夹中,但它没有显示在imageView中,也没有显示Toast。我已经尝试过使用几乎所有方法,比如使用不同的XML文件和更改类,但问题仍然存在。

package com.example.mohammadhasham.jtbopencameramodule;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Date;

public class CameraActivity extends AppCompatActivity {

    ImageView img;
    String currentPath;
    File f;

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

        img = (ImageView) (findViewById(R.id.capture));


    }

    public void captureImage(View v) {

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "image.jpg");
        Uri u = Uri.fromFile(f);
        i.putExtra(MediaStore.EXTRA_OUTPUT, u);
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(i, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                {
                    if (f.exists())
                    {
                        Toast.makeText(this, "Image Saved To Gallery", Toast.LENGTH_LONG).show();
                         Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath());
                          img.setImageBitmap(b);


                    }
                    else
                    {
                        Toast.makeText(this, "Image Not Saved To Gallery", Toast.LENGTH_LONG).show();

                    }
                    break;
                }
                case Activity.RESULT_CANCELED:
                {
                    break;
                }
                default:
                    break;
            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

我相信你的代码中有错误:

你说

if (resultCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                {

您将resultCode限制为值0,然后尝试根据其值执行切换。

答案 1 :(得分:0)

试试这个:

公共类CameraActivity扩展了AppCompatActivity {

private final String APP_TAG = "MyCustomApp";
private static final int REQUEST_IMAGE_CAPTURE = 1;
private String photoFileName = "photo.jpg";

private ImageView img;

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


    img = (ImageView) (findViewById(R.id.imageView));
}

public void captureImage(View v) {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
        takePictureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Toast.makeText(this, "Image Saved To Gallery", Toast.LENGTH_LONG).show();

        Uri takenPhotoUri = getPhotoFileUri(photoFileName);
        Bitmap imageBitmap = BitmapFactory.decodeFile(takenPhotoUri.getPath());
        img.setImageBitmap(imageBitmap);
    } else {
        Toast.makeText(this, "Image Not Saved To Gallery", Toast.LENGTH_LONG).show();

    }
}

// Returns the Uri for a photo stored on disk given the fileName
public Uri getPhotoFileUri(String fileName) {
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
        Log.d(APP_TAG, "failed to create directory");
    }

    // Return the file target for the photo based on filename
    return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + fileName));
}

}