Java 如何从图库中拾取图像,裁剪并将其保存在数据分区中
我需要一些帮助 我想从gallery中选取一个图像,然后将其重新调整为自定义大小,例如480*800,然后将其放入数据分区的文件夹中(例如(/data/test_wall))。 我试了很多次谷歌搜索,但都没有 这是我到目前为止的代码,但我无法从图库中选择并保存图像给我强制关闭:Java 如何从图库中拾取图像,裁剪并将其保存在数据分区中,java,android,android-intent,bitmap,gallery,Java,Android,Android Intent,Bitmap,Gallery,我需要一些帮助 我想从gallery中选取一个图像,然后将其重新调整为自定义大小,例如480*800,然后将其放入数据分区的文件夹中(例如(/data/test_wall))。 我试了很多次谷歌搜索,但都没有 这是我到目前为止的代码,但我无法从图库中选择并保存图像给我强制关闭: package com.example.walpaperpicker; import java.io.FileOutputStream; import android.os.Bundle; import android
package com.example.walpaperpicker;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int PICK_FRPM_GALLERY = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PickPic();
}
private void PickPic() {
// this code working for pick a picture with camera
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PICK_FRPM_GALLERY);
// this code not working give me fc after i select picture from gallery see my logcat
/*Intent GallaryIntent = new Intent();
GallaryIntent.setType("image/*");
GallaryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(GallaryIntent, PICK_FRPM_GALLERY);*/
}
});
}
@SuppressLint("SdCardPath")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FRPM_GALLERY) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView test = (ImageView) findViewById(R.id.imageView1);
test.setImageBitmap(photo);
try {
//I want put it in data partition but i dont know this is for sdcard
FileOutputStream out = new FileOutputStream("/sdcard/wallpaper.png");
photo.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*i want crop output picture
*to custom size for example 480*800
*but i dont know.
*/
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.walpaperpicker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"> </uses-permission>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"> </uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.walpaperpicker.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
谢谢你的帮助
我正在等待。我已经写了几篇关于如何选择画廊图像或缩略图的文章,这将对您有所帮助 及 好的,让我试着解释一下它是如何工作的 1。我们需要创建从Gallary中挑选图像的意图
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore
.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Bitmap createImageThumbnail(String imagePath, int width, int height) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(imagePath, bmpFactoryOptions);
return bitmap;
}
2。在同一活动中,我们需要使用onActivityResult方法处理拾取的图像
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE &&
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();
imageView = (ImageView) findViewById(R.id.imageView);
//here you can call createImageThumbnail method passing (picturePath,480,800)
//and set the received bitmap imageview directly instead of storing in bitmap.
// eg. imageView.setImageBitmap(createImageThumbnail( picturePath, 480, 800));
imageView.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
}
}
Bitmap createImageThumbnail(String imagePath, int width, int height) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(imagePath, bmpFactoryOptions);
return bitmap;
}
3。根据需要调整图像大小(宽度*高度)
Bitmap createImageThumbnail(String imagePath, int width, int height) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(imagePath, bmpFactoryOptions);
return bitmap;
}
如果您在这方面遇到任何问题,请告诉我。很乐意帮助。谢谢,伙计,我尝试了您的代码,但在重新调整大小方面遇到了问题,我想您忘记了我想将图片放入数据分区的文件夹
//imageView.setImageBitmap(createimage缩略图(picturePath,480800))代码>bmpFactoryOptions.inJustDecodeBounds=false;if(bitmap!=null){bitmap.recycle();bitmap=null;}bitmap=BitmapFactory.decodeFile(imagePath,bmpFactoryOptions);返回位图;//我的位图和imagePath有错误(这不应该是“picturePath”?)不能在上面的行中让我编译}