Android: сделать фото и сохранить в папке приложения
Задача: по кнопке сделать в приложении фото, и сохранить его с уникальным именем в папку приложения.
Решение:
1) Поправим файл AndroidManifest.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="eee.photossinhro"> <uses-feature android:name="android.hardware.camera" android:required="true" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="eee.fileprovider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> ... </application> </manifest> |
2) Добавим файл /xml/filepath.xml в папке res:
1 2 3 4 5 |
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="photossinhro" path="Android/data/eee.photossinhro/files/Pictures" /> </paths> |
3) Код приложения примерно следующий:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package eee.photossinhro; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v4.content.FileProvider; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.CardView; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import static android.media.tv.TvTrackInfo.TYPE_VIDEO; public class PhotosActivity extends AppCompatActivity { String id; //идентификатор аккаунта private Context cntcur; static final int REQUEST_PICTURE_CAPTURE = 1; private ImageView image; private String pictureFilePath; private File getPictureFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); String pictureFile = "photossinhro" + timeStamp; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile(pictureFile, ".jpg", storageDir); pictureFilePath = image.getAbsolutePath(); Log.i("Info", "--путь:"+pictureFilePath); return image; } private void sendTakePictureIntent() { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra( MediaStore.EXTRA_FINISH_ON_COMPLETION, true); if (cameraIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(cameraIntent, REQUEST_PICTURE_CAPTURE); File pictureFile = null; try { pictureFile = getPictureFile(); } catch (IOException ex) { Toast.makeText(this, "Photo file can't be created, please try again", Toast.LENGTH_SHORT).show(); return; } if (pictureFile != null) { Uri photoURI = FileProvider.getUriForFile(this, "eee.photossinhro.fileprovider", pictureFile); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(cameraIntent, REQUEST_PICTURE_CAPTURE); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photos); id = getIntent().getExtras().getString("id"); Log.i("Info","--мы в новой активности! ID="+id); cntcur=this; // нажата кнопка "сделать фото" Button button = (Button) findViewById(R.id.CreatePhotos); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("Info", "--нажата кнопка сделать фото"); if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ sendTakePictureIntent(); } } }); } } |