相机功能真的让我抓狂!我有活动A和活动B,其中所选图像将从活动B返回到A.我使用下面的代码,但它不适用于所有情况。某些选定的图像可以显示在活动A中,但有些不是。
活动B
b = (ImageView) findViewById(R.id.imageView3);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// Take photo
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
// h=1;
//imgui = selectedImage;
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image ******", picturePath + "");
b.setImageBitmap(thumbnail);
}
}
else
{
finish();
}
}
}
ok.setOnClickListener(new View.OnClickListener() // Return to Activity A
{
public void onClick(View arg0)
{
Intent returnIntent=new Intent();
b.setDrawingCacheEnabled(true);
b.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
b.layout(0, 0, b.getMeasuredWidth(), b.getMeasuredHeight());
b.buildDrawingCache(true);
returnIntent.putExtra("text", text);
if (b.getDrawingCache() != null) {
Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
if (bitmap == null) {
Log.e("TAG", "getDrawingCache() == null");
}
Global.img = bitmap;
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
活动A
viewImage = (ImageView) findViewById(R.id.imageView2);
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
if(requestCode==PROJECT_REQUEST_CODE) {
if(data!=null&&data.hasExtra("text")) {
c = data.getStringExtra("text");
txt1.setText(c);
viewImage.setImageBitmap(Global.img); // Not all image can be displayed here !!!
}
}
else if (requestCode==CAMERA_REQUEST_CODE)
{
}
}
}
Global.java
public class Global {
static Bitmap img;
}
11-08 01:39:19.820 8675-8675 / com.example.project.project W / path of image ******:/ storage/extSdCard/DCIM/100MSDCF/DSC00050.jpg 11-08 01:39:27.730 8675-8675 / com.example.project.project W / View:查看 大到适合绘制缓存,需要12582912字节,只有3686400 可用11-08 01:39:27.730 8675-8675 / com.example.project.project W / View:视图太大而无法适应绘图缓存,需要12582912 字节,只有3686400可用
activity_b
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="574dp"
android:layout_height="523dp"
android:id="@+id/imageView3"
android:layout_x="6dp"
android:layout_y="0dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="66dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText38"
android:layout_x="4dp"
android:hint="Add caption"
android:layout_y="491dp" />
<Button
android:layout_width="174dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button15"
android:layout_x="8dp"
android:layout_y="571dp" />
<Button
android:layout_width="174dp"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/button16"
android:layout_x="195dp"
android:layout_y="571dp" />
</AbsoluteLayout>
activity_a
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="47dp"
android:id="@+id/textView1"
android:background="#000000"
android:padding="5dp"
android:text="Add Project"
android:textColor="#FFFFFF"/>
<TextView
android:layout_width="170dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Project"
android:id="@+id/textView48"
android:layout_x="47dp"
android:layout_y="82dp" />
<EditText
android:layout_width="210dp"
android:layout_height="wrap_content"
android:hint="Amount"
android:id="@+id/editText36"
android:layout_x="47dp"
android:layout_y="133dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button17"
android:layout_x="47dp"
android:layout_y="560dp" />
<ImageButton
android:layout_width="66dp"
android:layout_height="54dp"
android:id="@+id/imageButton"
android:src="@mipmap/camerabutton"
android:layout_x="277dp"
android:layout_y="149dp"
xmlns:android="http://schemas.android.com/apk/res/android" />
<ImageView
android:layout_width="293dp"
android:layout_height="283dp"
android:id="@+id/imageView2"
android:layout_x="41dp"
android:layout_y="223dp" />
<TextView
android:layout_width="304dp"
android:layout_height="51dp"
android:id="@+id/textView57"
android:layout_x="43dp"
android:layout_y="490dp" />
</AbsoluteLayout>
首先将所选图像放入imageView3
(活动B)。单击button16
后,图像将显示在imageView2
。(活动A)
答案 0 :(得分:0)
尝试在ImageView
中设置缩放位图。通过这种方式,您将获得按比例缩小的Bitamp。
因此,在您将Bitmap
设置为ImageView
的代码中,请调用getScaledBitmap
并根据需要提供宽度和高度的输入参数。
private Bitmap getScaledBitmap(String picturePath, int width, int height) {
BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
sizeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picturePath, sizeOptions);
int inSampleSize = calculateInSampleSize(sizeOptions, width, height);
sizeOptions.inJustDecodeBounds = false;
sizeOptions.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(picturePath, sizeOptions);
}
private int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
根据您的getScaledBitmap()
height
和width
来呼叫View
image.setImageBitmap(getScaledBitmap(picturePath, WIDTH, HEIGHT));
希望它有所帮助。