我正在制作一个项目,其中我拍摄了两个ImageView,我可以通过从图库中选择那些图像在该图像视图上设置两个不同的图像。现在这里是一个视图,其中屏幕上有两个图像。我希望这个视图将我的画廊保存为单个图像... 我可以知道怎么办?
我使用的代码是......
在xml ....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView21"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView22"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
</LinearLayout>
在活动类中:
public class LayoutDisplay2 extends Activity{
ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
iv1 = (ImageView) findViewById(R.id.imageView21);
iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
iv2 = (ImageView) findViewById(R.id.imageView22);
iv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(in, RESULT_LOAD_IMAGE2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && 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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try{
iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}catch(Exception e){
e.printStackTrace();
}
}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Toast.makeText(getApplicationContext(), "in second",
Toast.LENGTH_SHORT).show();
Log.i("Second", "in second");
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try{
iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
UPDATE1:
我使用了这段代码,但没有在图库中保存布局:
public class LayoutDisplay2 extends Activity {
Button save;
LinearLayout ll;
ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ll = (LinearLayout) findViewById(R.id.linear2);
save = (Button) findViewById(R.id.savelayout2);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ll.setDrawingCacheEnabled(true);
Bitmap bitmap = ll.getDrawingCache();
String root = Environment.getExternalStorageDirectory()
.toString();
File newDir = new File(root + "/saved_picture");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = n + ".jpg";
File file = new File(newDir, fotoname);
String s = file.getAbsolutePath();
System.err.print("Path of saved image." + s);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
}
}
});
iv1 = (ImageView) findViewById(R.id.imageView21);
iv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
iv2 = (ImageView) findViewById(R.id.imageView22);
iv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(in, RESULT_LOAD_IMAGE2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && 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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try {
iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
} catch (Exception e) {
e.printStackTrace();
}
}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Toast.makeText(getApplicationContext(), "in second",
Toast.LENGTH_SHORT).show();
Log.i("Second", "in second");
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try {
iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
in xml ...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<ImageView
android:id="@+id/imageView21"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView22"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<RelativeLayout android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
>
<Button
android:id="@+id/savelayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Save" />
</RelativeLayout>
</LinearLayout>
我使用此代码检查它是否正在尝试或捕获
Log.i("Path of saved image.", s);
System.err.print("Path of saved image." + s);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
Toast.makeText(getApplicationContext(), "Photo Saved", Toast.LENGTH_SHORT).show();
out.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Photo Saved", Toast.LENGTH_SHORT).show();
Log.e("Exception", e.toString());
}
}
});
在logcat中我得到了..
04-17 13:19:33.908: I/Path of saved image.(30459): /mnt/sdcard/saved_picture/3400.jpg
04-17 13:19:33.918: E/Exception(30459): java.io.FileNotFoundException: /mnt/sdcard/saved_picture/3400.jpg (No such file or directory)
答案 0 :(得分:2)
将LinearLayout
设为setDrawingCacheEnabled(true);
并捕获布局并将其转换为Bitmap
并将其另存为sdcard中的图像。
在Button
点击中写下以下代码。
试试下面的代码。
public class LayoutDisplay2 extends Activity{
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ll=(LinearLayout)findViewById(R.id.linearlayout);
//Add button in your layout and write the below code onclick of button.
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ll.setDrawingCacheEnabled(true);
Bitmap bitmap = ll.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/saved_picture");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = n + ".jpg";
File file = new File(newDir, fotoname);
String s = file.getAbsolutePath();
System.err.print("Path of saved image." + s);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
}
}
});
清单中的:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>