سؤال

هنا هو رمز الكاميرا الخاصة بي.أنا أكتب وظيفة والتي تستخدم startActivityForResult للتعامل مع النتيجة.ولكن عندما أختار ACTION_IMAGE_CAPTURE, ، عادةً ما يتم التقاط الصورة ويمكنني العثور على مسار هذه الصورة لاستخدامه.ومع ذلك، لا يمكنني العثور على المسار في هاتفي المحمول.يبدو أن هذا الرمز لا يمكن استخدامه في هاتفي وبالتالي لا أستطيع حفظ الصورة في هاتفي المحمول.

طراز هاتفي المحمول هو 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);
}   

شكرا جزيلا لمساعدتكم!

هل كانت مفيدة؟

المحلول

هذا هو الكود الخاص بي لتظهر لك.

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;
}

نأمل أن تساعدك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top