2016-10-18 7 views
0

私は自分のアプリケーションでカメラを使って写真を撮ったときに自分のアプリケーション専用のアルバムを生成したいと思います。このアルバムは、アプリケーション内でカメラの意図からキャプチャしたすべての画像を保存するために使用されます。これはMessengerとWhatsappアプリのように機能します。誰でもこれを行う方法を知っていますか?MessengerとWhatsappアプリのようにカメラから画像を保存する自分のアプリアルバムを作成

public class ImageQueryActivity extends AppCompatActivity { 

    private LinearLayout mImageQueryLayout; 
    private ImageView mAddPhotoButton; 
    private String mCurrentPhotoPath; 

    final int REQUEST_IMAGE_CAPTURE = 1; 
    static final int REQUEST_GALLERY = 2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_image_query); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       dispatchTakePictureIntent(); 
      } 
     }); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mImageQueryLayout = (LinearLayout) findViewById(R.id.image_query_layout); 
     mAddPhotoButton = (ImageView) findViewById(R.id.add_photo_image_view); 
     mAddPhotoButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dispatchTakePictureIntent(); 
      } 
     }); 
    } 

    // dp to pixel converter 
    public static int convDpToPx(Context context, float dp) { 
     DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 
     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics); 
    } 

    private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       storageDir  /* directory */ 
     ); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = image.getAbsolutePath(); 
     return image; 
    } 

    private void dispatchTakePictureIntent() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(ImageQueryActivity.this); 
     builder.setPositiveButton("Camera", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
        File photoFile = null; 
        try { 
         photoFile = createImageFile(); 
        } catch (IOException ex) { 
         Toast.makeText(getApplicationContext(), 
           "Problem occurred while saving photo", 
           Toast.LENGTH_LONG).show(); 
        } 
        if (photoFile != null) { 
         Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), 
           "com.kelvin.foodizzy.fileprovider", 
           photoFile); 
         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
         startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
         galleryAddPic(mCurrentPhotoPath); 
        } 
       } 
      } 
     }); 
     builder.setNegativeButton("Gallery", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Intent intent = new Intent(); 
       intent.setType("image/*").setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, "Select File"), REQUEST_GALLERY); 
      } 
     }); 
     builder.show(); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == REQUEST_IMAGE_CAPTURE) { 
       ImageView imageView = new ImageView(this); 
       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams 
         (LinearLayout.LayoutParams.MATCH_PARENT, 
           convDpToPx(getApplicationContext(), 180)); 
       layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, 
         layoutParams.rightMargin, convDpToPx(getApplicationContext(), 20)); 
       imageView.setLayoutParams(layoutParams); 
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
       imageView.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath)); 
       galleryAddPic(mCurrentPhotoPath); 
       mImageQueryLayout.addView(imageView); 
      } 
     } 
    } 

    private void galleryAddPic(String file) { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(file); 
     Uri contentUri = Uri.fromFile(f); 
     mediaScanIntent.setData(contentUri); 
     sendBroadcast(mediaScanIntent); 
    } 

} 
+0

あなたは基本的にフォルダの作成方法を尋ねています。 –

+0

画像も保存したいです。現在のところ、Photosアプリで画像を取得できません。 以下は私のfile_paths.xmlです: –

+0

<?xml version = "1.0" encoding = "utf-8"?> <外部パス 名=" my_images」 パス= "アンドロイド/データ/ com.kelvin.foodizzy /ファイル/写真" />

答えて

0

最後に私は自分の問題を解決することができます。上記のAPIでのこの作業24

//set file storage to getExternalStoragePublicDirectory 
    private File createImageFile() throws IOException { 
      @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
      String imageFileName = "JPEG_" + timeStamp + "_"; 
      File storageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "Foodizzy"); 
      if (!storageDir.exists()) { 
       if (!storageDir.mkdirs()) { 
        Log.d("Camera", "failed to create directory"); 
        return null; 
       } 
      } 
      File image = File.createTempFile(
        imageFileName, 
        ".jpg", 
        storageDir 
      ); 
      mCurrentPhotoPath = image.getAbsolutePath(); 
      return image; 
     } 

//Inside camera intent, use file provider to get photo uri 
Intent takePictureIntent = new Intent(); 
    takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 


    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     File photoFile = null; 
    try { 
      photoFile = createImageFile(); 
    } catch (IOException ex) { 
      ex.printStackTrace(); 
    } 
    if (photoFile != null) { 
     Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), 
     BuildConfig.APPLICATION_ID + ".fileprovider", photoFile); 
     takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
     startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
    } 
     } 


//to store the image inside photo Gallery/(in my Nexus phone is Photos), create the following method 
private void galleryAddPic() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(mCurrentPhotoPath); 
     Uri contentUri = Uri.fromFile(f); 
     mediaScanIntent.setData(contentUri); 
     this.sendBroadcast(mediaScanIntent); 
     } 

//and call the method inside onActivityResult 
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      if (requestCode == REQUEST_IMAGE_CAPTURE) { 
       //code to get bitmap or whatever 
       galleryAddPic(); 

      } 
     } 
    } 

    //Inside AndroidManifest.xml, add provider inside application 
    <application 
      ... 
      ...> 
      <provider 
       android:name="android.support.v4.content.FileProvider" 
       android:authorities="${applicationId}.fileprovider" 
       android:exported="false" 
       android:grantUriPermissions="true"> 
       <meta-data 
        android:name="android.support.FILE_PROVIDER_PATHS" 
        android:resource="@xml/file_paths" /> 
      </provider> 
    </application> 

    //create a file named "file_paths.xml" under xml and add the following code 
    <?xml version="1.0" encoding="utf-8"?> 
    <paths> 
     <external-path name="external_files" path="."/> 
    </paths> 
+0

この回答があなたを助けたら投票してください。 –

関連する問題