无法从画廊上传图片 [英] Can't upload image from Gallery
问题描述
我想,让用户要么选择从画廊的照片或图像视图点击在使用相机拍照。我跟着几个在线教程,这里是我的尝试:
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageView;
进口android.widget.Toast;
公共类UploadImageActivity延伸活动{
私人最终诠释CAMERA_PICTURE = 1;
私人最终诠释GALLERY_PICTURE = 2;
私人ImageView的userPictureImageView;
私人意图pictureActionIntent = NULL;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
userPictureImageView =(ImageView的)findViewById(R.id.image_view);
userPictureImageView.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
startDialog();
}
});
}
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
如果(要求code == GALLERY_PICTURE){
开放的我们的uri = data.getData();
如果(URI!= NULL){
//用户必须选择一个图像。
光标光标= getContentResolver()查询(URI,新的String [] {} android.provider.MediaStore.Images.ImageColumns.DATA,NULL,NULL,NULL);
cursor.moveToFirst();
//链接到图片
最后弦乐imageFilePath = cursor.getString(0);
档案照片=新的文件(imageFilePath);
位图B =去codeFILE(照片);
B = Bitmap.createScaledBitmap(二,150,150,真);
userPictureImageView.setImageBitmap(B);
cursor.close();
}
其他 {
吐司面包= Toast.makeText(这一点,没有图像被选中。Toast.LENGTH_LONG);
toast.show();
}
}
否则,如果(要求code == CAMERA_PICTURE){
如果(data.getExtras()!= NULL){
//这里是相机的图像
。点阵位图=(位图)data.getExtras()获得(数据);
userPictureImageView.setImageBitmap(位);
}
}
}
私人位图德codeFILE(文件f){
尝试 {
//德code图像尺寸
BitmapFactory.Options O =新BitmapFactory.Options();
o.inJustDe codeBounds = TRUE;
BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O);
//找到正确的比例值。它应该是2的幂。
最终诠释REQUIRED_SIZE = 70;
INT width_tmp = o.outWidth,height_tmp = o.outHeight;
int标= 1;
而(真){
如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2'; REQUIRED_SIZE)
打破;
width_tmp / = 2;
height_tmp / = 2;
规模++;
}
//德code与inSampleSize
BitmapFactory.Options O2 =新BitmapFactory.Options();
o2.inSampleSize =规模;
返回BitmapFactory.de codeStream(新的FileInputStream(f)项,空,O2);
}
赶上(FileNotFoundException异常E){
}
返回null;
}
私人无效startDialog(){
AlertDialog.Builder myAlertDialog =新AlertDialog.Builder(本);
myAlertDialog.setTitle(上传图片选项);
myAlertDialog.setMessage(你要设置你的照片?);
myAlertDialog.setPositiveButton(画廊,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface为arg0,INT ARG1){
pictureActionIntent =新的意图(Intent.ACTION_GET_CONTENT,NULL);
pictureActionIntent.setType(图像/ *);
pictureActionIntent.putExtra(回归数据,真正的);
startActivityForResult(pictureActionIntent,GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton(摄像机,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface为arg0,INT ARG1){
pictureActionIntent =新的意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent,CAMERA_PICTURE);
}
});
myAlertDialog.show();
}
}
该相机工作正常,但库并没有在所有的工作。它不停地崩溃的一些原因,任何人都可以摆脱我的光?谢谢你。
修改
错误日志
1月十一号日至14日:09:30.566:E / AndroidRuntime(24630):致命异常:主要
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 2,结果为0,数据= NULL}到活动{com.csun。 spotr / com.csun.spotr.UploadImageActivity}:显示java.lang.NullPointerException
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.ActivityThread.access $ 2000(ActivityThread.java:117)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:961)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.os.Handler.dispatchMessage(Handler.java:99)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.os.Looper.loop(Looper.java:130)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.ActivityThread.main(ActivityThread.java:3683)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在java.lang.reflect.Method.invokeNative(本机方法)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在java.lang.reflect.Method.invoke(Method.java:507)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在dalvik.system.NativeStart.main(本机方法)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):由:显示java.lang.NullPointerException
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在com.csun.spotr.UploadImageActivity.onActivityResult(UploadImageActivity.java:42)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.Activity.dispatchActivityResult(Activity.java:3908)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):在android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
1月一十一号日至14日:09:30.566:E / AndroidRuntime(24630):11 ...更多
刚刚意识到,如果我走这条线了:
super.onActivityResult(要求code,因此code,数据);
那么它不再崩溃,但点击从画廊的图像上并没有弹出的图像视图。你知道吗?
私有静态最终诠释SELECT_PICTURE = 1;
私人字符串selectedImagePath,filemanagerstring;
私有静态文件MYFILE = NULL;
意向意图=新的意图();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(意向,
选择图片),SELECT_PICTURE);
onActivity导致::
如果(数据!= NULL){
乌里selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
如果(selectedImagePath!= NULL)
MYFILE =新的文件(selectedImagePath);
否则,如果(filemanagerstring!= NULL)
MYFILE =新的文件(filemanagerstring);
如果(MYFILE!= NULL){
位图bmp_fromGallery =去codeImageFile(selectedImagePath);
的System.out.println(位图是::+ myPhoto_bitmap);
//将位图的ImageView
} 其他 {
Toast.makeText(getApplicationContext(),
myFile.getName()+为空,Toast.LENGTH_LONG)
。显示();
}
} 其他 {
Toast.makeText(SelectPhoto.this,请选择图片!,
Toast.LENGTH_LONG).show();
}
I want to let the user either choose a picture from Gallery or take a picture using Camera when clicking on the image view. I followed several tutorials online, and here is my attempt:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class UploadImageActivity extends Activity {
private final int CAMERA_PICTURE = 1;
private final int GALLERY_PICTURE = 2;
private ImageView userPictureImageView;
private Intent pictureActionIntent = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userPictureImageView = (ImageView) findViewById(R.id.image_view);
userPictureImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startDialog();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
Uri uri = data.getData();
if (uri != null) {
// User had pick an image.
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
// Link to the image
final String imageFilePath = cursor.getString(0);
File photos = new File(imageFilePath);
Bitmap b = decodeFile(photos);
b = Bitmap.createScaledBitmap(b, 150, 150, true);
userPictureImageView.setImageBitmap(b);
cursor.close();
}
else {
Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
toast.show();
}
}
else if (requestCode == CAMERA_PICTURE) {
if (data.getExtras() != null) {
// here is the image from camera
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
userPictureImageView.setImageBitmap(bitmap);
}
}
}
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
}
catch (FileNotFoundException e) {
}
return null;
}
private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent, CAMERA_PICTURE);
}
});
myAlertDialog.show();
}
}
The Camera works fine, but the Gallery doesn't work at all. It kept crashing for some reasons, could anyone shed me a light? Thanks.
EDIT
Error Log
11-14 01:09:30.566: E/AndroidRuntime(24630): FATAL EXCEPTION: main
11-14 01:09:30.566: E/AndroidRuntime(24630): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.csun.spotr/com.csun.spotr.UploadImageActivity}: java.lang.NullPointerException
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.os.Looper.loop(Looper.java:130)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 01:09:30.566: E/AndroidRuntime(24630): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 01:09:30.566: E/AndroidRuntime(24630): at java.lang.reflect.Method.invoke(Method.java:507)
11-14 01:09:30.566: E/AndroidRuntime(24630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 01:09:30.566: E/AndroidRuntime(24630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 01:09:30.566: E/AndroidRuntime(24630): at dalvik.system.NativeStart.main(Native Method)
11-14 01:09:30.566: E/AndroidRuntime(24630): Caused by: java.lang.NullPointerException
11-14 01:09:30.566: E/AndroidRuntime(24630): at com.csun.spotr.UploadImageActivity.onActivityResult(UploadImageActivity.java:42)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
11-14 01:09:30.566: E/AndroidRuntime(24630): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
11-14 01:09:30.566: E/AndroidRuntime(24630): ... 11 more
Just realized that if I take this line out:
super.onActivityResult(requestCode, resultCode, data);
then it no longer crash, but clicking on the image from Gallery didn't pop up on the image view. Any idea?
private static final int SELECT_PICTURE = 1;
private String selectedImagePath, filemanagerstring;
private static File myFile = null;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
onActivity result::
if (data != null) {
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);
System.out.println("Bitmap is :: " + myPhoto_bitmap);
//SET BITMAP TO IMAGEVIEW
} else {
Toast.makeText(getApplicationContext(),
myFile.getName() + "is null", Toast.LENGTH_LONG)
.show();
}
} else {
Toast.makeText(SelectPhoto.this, "Please Select Image!!!",
Toast.LENGTH_LONG).show();
}
这篇关于无法从画廊上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!