2014-06-04 4 views
0

Вот мой код камеры. Я пишу функцию, которая использует startActivityForResult для выполнения результата. Но когда я выбираю ACTION_IMAGE_CAPTURE, обычно он будет захватывать рис, и я могу пойти и найти этот путь для этой ссылки. Однако я не могу найти путь на своем мобильном телефоне. Кажется, этот код не может использоваться в моем телефоне, и поэтому я не могу сохранить pic в своем мобильном телефоне.Моя функция камеры не работает

Моя модель мобильного телефона Sony Xperia C и Sony Xperia L.

Вот мой выбор кода

public void function() 
{ 
    AlertDialog.Builder builder = new AlertDialog.Builder(repair.this); 
    builder.setTitle("Please select").setPositiveButton("camera", new DialogInterface.OnClickListener() 
    {  
     public void onClick(DialogInterface dialog, int i) 
     {  
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, 1); 
     }  
    }).setNegativeButton("album", new DialogInterface.OnClickListener() 
    {   
     public void onClick(DialogInterface dialog, int i) 
     {    
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(intent, 2);    
     } 
    }).show(); 
} 

А вот мой onActivityResult код.

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    options.inSampleSize = calculateInSampleSize(options, 2048, 2048); 
    options.inJustDecodeBounds = false; 

    if (resultCode == RESULT_OK) 
    { 
     if (requestCode == 1 || requestCode == 2) 
     { 
      Uri photoUri = data.getData(); 
      String[] pojo = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = managedQuery(photoUri, pojo, null, null,null);  
      if(cursor != null) 
      { 
       int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); 
       cursor.moveToFirst(); 
       picPath = cursor.getString(columnIndex); 
      }    
      if(picPath != null && (picPath.endsWith(".png") || picPath.endsWith(".PNG") ||picPath.endsWith(".jpg") ||picPath.endsWith(".JPG") )) 
      { 
        picPath1 = picPath; 
        Bitmap bm = BitmapFactory.decodeFile(picPath1, options); 
        imgbtnphoto1.setImageBitmap(bm); 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();     
        bm.compress(CompressFormat.JPEG, 80, bos); 
        byte[] data1 = bos.toByteArray(); 
        bab1 = new ByteArrayBody(data1, "img1.jpg"); 
      } 
      else 
      { 
       Toast.makeText(this, "Picture type is incorrect", Toast.LENGTH_LONG).show(); 
      } 
     } 
     else 
     { 
      Toast.makeText(this, "Please re-select the picture", Toast.LENGTH_LONG).show(); 
     } 
    } 
    else 
    { 
     Toast.makeText(this, "Please re-select the picture", Toast.LENGTH_LONG).show(); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

Большое вам спасибо за помощь!

+0

Удалось ли найти решение этой проблемы? Я столкнулся с этой проблемой на моем Sony Xperia C –

+0

Я закончил. Спасибо. – Mozart

+0

Можете ли вы разместить свое решение здесь? Ответьте на свой вопрос, а затем примите его. Это будет очень полезно для людей, столкнувшихся с одной и той же проблемой. –

ответ

0

Это мой код для показа.

public void initView() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(repair.this).setMessage("Select") 
     .setPositiveButton("Camera", new DialogInterface.OnClickListener() {   
     public void onClick(DialogInterface dialog, int i) {   
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      photoUri = getOutputMediaFileUri(3); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);    
      startActivityForResult(intent, 1); 
     }  
    }).setNegativeButton("Album", new DialogInterface.OnClickListener() {   
     public void onClick(DialogInterface dialog, int i) {     
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(intent, 2);    
     } 
    }); 
    AlertDialog dialog = builder.show(); 
    TextView messageText = (TextView)dialog.findViewById(android.R.id.message); 
    messageText.setGravity(Gravity.CENTER); 
    dialog.show(); 
} 

public Uri getOutputMediaFileUri(int type){ 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

private static File getOutputMediaFile(int type) { 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Camera"); // Storage Folder 

    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d("Camera", "Oops! Failed create "+ "Camera" + " directory"); // Error warning 
      return null; 
     } 
    } 

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); 
    File mediaFile; 
    if (type == 3) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); //Storage Name 
    } else { 
     return null; 
    } 
    return mediaFile; 
} 

Надеюсь, это может вам помочь.

+0

где вы использовали эту строку кода выше? 'getContentResolver(). insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, значения);' –

+0

Я использую 'getOutputMediaFileUri (3);' replace' getContentResolver(). insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, значения); ' – Mozart

Смежные вопросы