从图库中选择图像,在imageview中显示并保存在新目录中显示的图像
问题描述:
我在我的Activity中有两个按钮和一个ImageView。当我点击一个按钮时,它会重定向到图库,我可以选择一个图像。所选图像将显示在我的ImageView中。 现在的问题是我想要保存按钮来保存图像显示在一个新的目录,就像它在像instagram一样的应用程序中。从图库中选择图像,在imageview中显示并保存在新目录中显示的图像
我的布局如下:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="imgClick"
android:id="@+id/imgButton"
android:text="Select Image"
android:layout_marginTop="20dp" />
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_centerHorizontal="true"
android:id="@+id/diddyImageView"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:maxWidth="100dp"
android:maxHeight="100dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addMovieClick"
android:id="@+id/addMovieButton"
android:text="Submit Movie"
android:background="@color/colorPrimary"
android:layout_marginTop="20dp" />
我的活动如下
public void imgClick(View v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECTED_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECTED_PICTURE && resultCode == RESULT_OK) {
uri = data.getData();
diddyImageView.setImageURI(uri);
}
}
图像给出正确显示,我想将图像保存在一个新的文件夹,我不知道如何去帮助it..Any会appreciated..Thanks
答
第1步:使用ContentResolver
和openInputStream()
要打开Uri
的InputStream
到图像
第2步:打开所需的文件FileOutputStream
为您的副本
第3步:使用标准的Java I/O从InputStream
字节复制到FileOutputStream
第4步:将所有的这工作到后台线程(并考虑使用图像加载库来取代你的setImageURI()
调用),以避免捆绑主要应用程序线程做所有这些工作
'我想保存图像在一个新的文件夹'如何使用简单的文件副本? –