我的应用:拍照并将它们存放在外部存储器的文件夹中。当我从应用程序外部进入文件夹时,我发现这些照片已经注册,但当我从应用程序进入该文件夹时,没有显示任何内容。
CameraActivity.java:
public class CameraActivity extends Activity {
Button button ;
ImageView imageView ;
Button button2 ;
static final int CAM_REQUEST = 1 ;
File[] f ;
int cpt;
File folder = new File("/sdcard/Camera_app");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
button = (Button) findViewById(R.id.CameraButton);
imageView = (ImageView) findViewById(R.id.imageView);
button2 = (Button) findViewById(R.id.dossier);
f = folder.listFiles();
try {cpt = f.length ;} catch (NullPointerException e){cpt=1;}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Camera_intenet = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
Camera_intenet.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(file));
startActivityForResult(Camera_intenet, CAM_REQUEST);}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent acces_intent = new Intent(Intent.ACTION_PICK);
File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] listOfPictures = pictures.list();
Uri uri = null ;
ArrayList<Uri> arrayList = new ArrayList<>();
for (String picture : listOfPictures)
{
uri = Uri.parse("file://"+pictures.toString()+"/"+picture);
arrayList.add(uri);}
acces_intent.setType("image/*");
acces_intent.putExtra(acces_intent.EXTRA_STREAM, arrayList);
startActivityForResult(acces_intent, 1);
}
});}
private File getFile()
{
if(!folder.exists())
{folder.mkdir();}
File image_file = new File(folder,"Cam_image"+cpt+".jpg");
return image_file ;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "/sdcard/Camera_app/Cam_image"+cpt+".jpg";
cpt++;
imageView.setImageDrawable(Drawable.createFromPath(path));
答案 0 :(得分:2)
您是否已将权限<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
添加到AndroidManifest?