Java Android-检索拾取图像的路径
我有java代码从画廊或相机中挑选图像。在这段代码中,当用户选择一幅图像时,它会被裁剪成200x200大小。我正在寻找裁剪图像的绝对路径,我想将裁剪图像移动到SD卡中。但是我没有得到文件扩展名为的路径。当我尝试获取路径时,它返回的路径文件名包括随机数,如下所示Java Android-检索拾取图像的路径,java,android,android-intent,android-image,android-file,Java,Android,Android Intent,Android Image,Android File,我有java代码从画廊或相机中挑选图像。在这段代码中,当用户选择一幅图像时,它会被裁剪成200x200大小。我正在寻找裁剪图像的绝对路径,我想将裁剪图像移动到SD卡中。但是我没有得到文件扩展名为的路径。当我尝试获取路径时,它返回的路径文件名包括随机数,如下所示 /storage/media/865412 我无法移动此文件。我需要文件扩展名为的绝对路径 ProfileInfo.java public class ProfileInfo extends Activity { private
/storage/media/865412
我无法移动此文件。我需要文件扩展名为的绝对路径
ProfileInfo.java
public class ProfileInfo extends Activity {
private Uri mImageCaptureUri;
private ImageView mImageView;
private static final int PICK_FROM_CAMERA = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_info);
final String [] items = new String [] {"Take from camera", "Select from gallery"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) { //pick from camera
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { //pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
mImageView = (ImageView) findViewById(R.id.profile_pic);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_CAMERA:
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
mImageView.setImageBitmap(photo);
Toast.makeText(getApplicationContext(),
photo.getWidth()+"x"+photo.getHeight()+"\n"+photo.getByteCount()+"\n",
0).show();
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
Toast.makeText(getApplicationContext(),mImageCaptureUri.getPath(), 0).show();
break;
}
}
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
@Override
public void onCancel( DialogInterface dialog ) {
if (mImageCaptureUri != null ) {
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}
}
public class CropOptionAdapter extends ArrayAdapter<CropOption> {
private ArrayList<CropOption> mOptions;
private LayoutInflater mInflater;
public CropOptionAdapter(Context context, ArrayList<CropOption> options) {
super(context, R.layout.crop_selector, options);
mOptions = options;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup group) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.crop_selector, null);
CropOption item = mOptions.get(position);
if (item != null) {
((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
return convertView;
}
return null;
}
}
public class CropOption {
public CharSequence title;
public Drawable icon;
public Intent appIntent;
}
crop_selector.xml
<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:background="@color/activity_bg_color"
android:paddingBottom="0dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.beproject.ourway.ProfileInfo" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:alpha="0.7"
android:text="Tap on image to change (Optional)"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/profile_pic"
android:layout_width="156dp"
android:layout_height="156dp"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:src="@drawable/user" />
<EditText
android:id="@+id/your_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Your name..."
android:textAlignment="center"
android:textColorHint="#DFDFDF" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/skip_image_upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:background="@drawable/flat_button_selector"
android:text="Skip"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/image_upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="73dp"
android:background="@drawable/flat_button_selector"
android:onClick="uploadImage"
android:text="Upload"
android:textColor="#FFFFFF" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
复制文件不一定需要绝对文件路径,因为您可以直接从
Uri
打开InputStream
,然后从中复制内容:
void copyFileFromUri(Uri sourceUri, String destFilePath) throws IOException
{
InputStream in = getContentResolver().openInputStream(sourceUri);
File outFile = new File(destFilePath);
OutputStream out = new FileOutputStream(outFile);
copyFile(in, out);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
copyFile()函数从answer复制到
我正在寻找裁剪图像的绝对路径
。裁剪后的图像没有路径,因为在ActivityResult中得到的是位图。所以你可以用位图做你想要的。它还没有被保存或裁剪。原始图像未受损/未剪切。嗯,这就是我所经历的。但是如何保存那张裁剪过的图像呢?我很困惑。你已经有位图了。所以将位图保存到文件中。这方面的代码已经在该网站上发布了很多次。您的解决方案也可以使用…谢谢您要使用绝对文件路径复制文件的地方,请调用此函数以使用Uri进行复制