이미지 뷰를 클릭하면 갤러리에서 사진을 선택하거나 카메라로 사진을 찍을 수 있습니다. 온라인에서 여러 자습서를 따랐으며 여기에 시도했습니다.갤러리에서 이미지를 업로드 할 수 없습니다.
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();
}
}
카메라는 정상적으로 작동하지만 갤러리가 전혀 작동하지 않습니다. 그것은 몇 가지 이유로 충돌을 계속했고, 누군가 내게 빛을 비춰 줄 수 있었습니까? 감사. super.onActivityResult(requestCode, resultCode, data);
다음 더 이상 충돌하지만 갤러리에서 이미지를 클릭하면 팝업하지 않았다 :
편집
오류 로그는
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
그냥 내가이 줄을 가지고가는 경우에 실현 이미지보기에서. 어떤 생각?
을 던집니다? –
@Samir : 휴대 전화에서만 수동으로 실행할 수 있으므로 유용한 메시지가 표시되지 않았습니다. 이미 내 sdcard를 설정했지만 에뮬레이터를 사용하면 "이미지를 찾을 수 없습니다"라는 메시지가 항상 나타납니다. – Chan
어떤 오류가 발생합니까 ??? 공유 오류 로그 –