gpt4 book ai didi

php - 返回 fragment 时,Android Camera API崩溃

转载 作者:行者123 更新时间:2023-12-03 17:26:54 26 4
gpt4 key购买 nike

因此,我的Android应用程序遇到了一些麻烦。此代码的目的是使用户能够单击按钮并拍照,该图片将显示在ImageView中,然后使用php脚本上传到服务器。

在我的设备上运行此命令可以使用户拍照,但随后无法返回到应用程序,就像未调用onActivityResult一样。我也尝试过在应用程序中运行第二个 Activity ,结果也相同。

public class CameraFragment extends Fragment implements OnClickListener {
private Button mTakePhoto;
private ImageView mImageView;
private static final String TAG = "upload";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentlayout, container, false);

return rootView;
}

@Override
public void onStart() {
super.onStart();

mTakePhoto = (Button) getView().findViewById(R.id.take_photo);
mImageView = (ImageView) getView().findViewById(R.id.imageview);

mTakePhoto.setOnClickListener(this);
}

@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.take_photo:
takePhoto();
break;
}
}

private void takePhoto() {
dispatchTakePictureIntent();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "onActivityResult: " + this);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
setPic();
}
}

private void sendPhoto(Bitmap bitmap) throws Exception {
new UploadTask().execute(bitmap);
}

private class UploadTask extends AsyncTask<Bitmap, Void, Void> {

protected Void doInBackground(Bitmap... bitmaps) {
if (bitmaps[0] == null)
return null;

Bitmap bitmap = bitmaps[0];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream); // convert Bitmap to ByteArrayOutputStream
InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert ByteArrayOutputStream to ByteArrayInputStream

DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(
"http://link-to-serverupload-phpscript"); // server

Random r = new Random();
int intRandom = r.nextInt(9999);
String prefix = "SSImg_" + System.currentTimeMillis() + "_" + intRandom + ".jpg";

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("myFile",prefix, in);
httppost.setEntity(reqEntity);

Log.i(TAG, "request " + httppost.getRequestLine());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (response != null)
Log.i(TAG, "response " + response.getStatusLine().toString());
} finally {

}
} finally {

}

if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return null;
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(getActivity(), "Success", Toast.LENGTH_LONG).show();
}
}

@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume: " + this);
}

@Override
public void onPause() {
super.onPause();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
}

String mCurrentPhotoPath;

static final int REQUEST_TAKE_PHOTO = 1;
File photoFile = null;

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
String storageDir = Environment.getExternalStorageDirectory() + "/picupload";
File dir = new File(storageDir);
if (!dir.exists())
dir.mkdir();

File image = new File(storageDir + "/" + imageFileName + ".jpg");

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
Log.i(TAG, "photo path = " + mCurrentPhotoPath);
return image;
}

private void setPic() {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();

// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor << 1;
bmOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

Matrix mtx = new Matrix();
//Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);

if (rotatedBMP != bitmap)
bitmap.recycle();

mImageView.setImageBitmap(rotatedBMP);

try {
sendPhoto(rotatedBMP);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

在研究了这个问题之后,我实现了 super.onActivityResult()方法,但是没有成功。
任何想法将不胜感激!

最佳答案

它可能只是片段的导入,而不是app.v4.fragment,如果没有运气,请使用另一个来打印logcat并在此处显示。

关于php - 返回 fragment 时,Android Camera API崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com