图像视图既不是从位图也不是从Uri设置的?

时间:2014-05-11 15:21:21

标签: android image android-layout bitmap android-camera

我正在学习android。我试图从相机拍摄照片,然后将其保存在特定文件夹“Myimages”中,并在ImageView中显示图像。当我从相机捕获图像时,图像保存在myimage文件夹中,但它没有显示在ImageView中。我建议给我一个解决方案。

1.layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/path"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <ImageView
        android:id="@+id/image_view"

        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="50dp"
        android:layout_toRightOf="@+id/path"
        android:maxHeight="42dp"
        android:maxWidth="42dp"
        android:scaleType="fitCenter" />

</RelativeLayout>

2.Mainactivity

public class MainActivity extends Activity {

private static final int REQUEST_CODE = 2;
File folder;
File imagefile;
TextView imagepath;

ImageView imageview;
Uri imageuri;
Intent intent;
String folderpath;
int ct = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageview = (ImageView) findViewById(R.id.image_view);
    imagepath = (TextView) findViewById(R.id.path);

    createfolder();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_settings) {

        imagefile = getimagefile(folder);
        imagepath.setText(imagefile.getAbsolutePath().toString());
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagefile));

        startActivityForResult(intent, REQUEST_CODE);

    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bp = (Bitmap) intent.getExtras().get("data");

    // imageview.setImageBitmap(bp);
    imageview.setImageURI(Uri.fromFile(imagefile));

}

public File getimagefile(File folder) {
    File mediafile;

    mediafile = new File(folder.getAbsoluteFile() + File.separator + "img_"
            + ct + ".jpeg");
    ct++;

    return mediafile;
}

public File createfolder() {
    folder = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    + "/Myimages");
    if (!folder.exists()) {
        folder.mkdir();
    }

    return folder;
}

}

3 个答案:

答案 0 :(得分:0)

尝试将Bitmap转换为BitmapDrawable,然后使用SetImageDrawable。

答案 1 :(得分:0)

Data Intent通过onActivityResult找到你可以获得图像路径并使文件然后使uri在imageView中设置imageUri。

String mTempImagePath = data.getData();
File file = new File(mTempImagePath .toString());
Uri uri = Uri.fromFile(file);
imageview.setImageURI(uri);

答案 2 :(得分:0)

// try this way,hope this will help you...

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imageview.setImageBitmap(decodeFile(imagefile.getAbsolutePath()));

    }

    public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 100;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }