尝试创建文件夹,但它指示 logcat 错误?怎么解决?

发布于 2024-11-04 14:31:39 字数 4728 浏览 1 评论 0原文

你好,现在我正在尝试在我的画廊中创建文件夹......所以我在我的 galleryview.java 文件......但没有任何需要更改的正常过程正在进行......请帮助我......

File nfile=new File(Environment.getExternalStorageDirectory()+"/picture");
nfile.mkdir();
  1. Galleryview.java

    public class Galleryview 扩展 Activity {
    /** 首次创建活动时调用。 */
    私有光标图像光标,实际图像光标;
    私有 int image_column_index,实际_image_column_index;
    GridView图像网格;
    私有整数计数;
    @覆盖
    公共无效onCreate(捆绑保存实例状态){
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        文件 nfile=new File(Environment.getExternalStorageDirectory()+"/图片");
        nfile.mkdir();
    
        init_phone_image_grid();
       }
    

这是我的完整源代码:

 package galleryview.galleryview;

 import java.io.File;

  //import ImageViewExample.ImageViewExample.ViewImage;
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.Gallery;
  import android.widget.GridView;
  //import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.AdapterView.OnItemClickListener;

  public class Galleryview extends Activity {
  /** Called when the activity is first created. */
  private Cursor imagecursor, actualimagecursor;
  private int image_column_index, actual_image_column_index;
  GridView imagegrid;
  private int count;
  @Override
  public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        File nfile=new File(Environment.getExternalStorageDirectory()+"/sdcard");
        nfile.mkdir();
        System.out.println("total"+Environment.getExternalStorageDirectory());

        init_phone_image_grid();
  }
  private void init_phone_image_grid() {

        String[] img = { MediaStore.Images.Thumbnails._ID };
        imagecursor = managedQuery(
  MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
  null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
        image_column_index = imagecursor
  .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        count = imagecursor.getCount();
        imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
        imagegrid.setOnItemClickListener(new OnItemClickListener(){
              public void onItemClick(AdapterView parent, View v,
   int position, long id) {
                    System.gc();
                    String[] proj = { MediaStore.Images.Media.DATA };
                    actualimagecursor = managedQuery(
   MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
   null, null, null);
                    actual_image_column_index = actualimagecursor
   .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    actualimagecursor.moveToPosition(position);
                    String i = actualimagecursor.getString(actual_image_column_index);
                    System.gc();
                   Intent intent = new Intent(getApplicationContext(),ViewImage.class);

                   intent.putExtra("filename", i);
                   startActivity(intent);
                                                            }
        });
       }


  public class ImageAdapter extends BaseAdapter {


      private Context mContext;
        public ImageAdapter(Context c) {
              mContext = c;
        }
        public int getCount() {
              return count;
        }
        public Object getItem(int position) {
              return position;
        }
        public long getItemId(int position) {
              return position;
        }
        public View getView(int position,View convertView,ViewGroup parent) {
              System.gc();
              ImageView i = new ImageView(mContext.getApplicationContext());
              if (convertView == null) {
                    imagecursor.moveToPosition(position);
                    int id = imagecursor.getInt(image_column_index);
                    i.setImageURI(Uri.withAppendedPath(
            MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));
                    i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    i.setLayoutParams(new GridView.LayoutParams(110,110));
              }
              else {
                    i = (ImageView) convertView;
              }
              return i;
          }
        }
         }

Hi Now I am trying to create folders in my gallery...so I am implement these line in my
galleryview.java file.......but nothing to be change normal process is going on....please help me.....

File nfile=new File(Environment.getExternalStorageDirectory()+"/picture");
nfile.mkdir();
  1. Galleryview.java

    public class Galleryview extends Activity {
    /** Called when the activity is first created. */
    private Cursor imagecursor, actualimagecursor;
    private int image_column_index, actual_image_column_index;
    GridView imagegrid;
    private int count;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        File nfile=new File(Environment.getExternalStorageDirectory()+"/picture");
        nfile.mkdir();
    
        init_phone_image_grid();
       }
    

This is my complete source code:

 package galleryview.galleryview;

 import java.io.File;

  //import ImageViewExample.ImageViewExample.ViewImage;
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.Gallery;
  import android.widget.GridView;
  //import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.AdapterView.OnItemClickListener;

  public class Galleryview extends Activity {
  /** Called when the activity is first created. */
  private Cursor imagecursor, actualimagecursor;
  private int image_column_index, actual_image_column_index;
  GridView imagegrid;
  private int count;
  @Override
  public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        File nfile=new File(Environment.getExternalStorageDirectory()+"/sdcard");
        nfile.mkdir();
        System.out.println("total"+Environment.getExternalStorageDirectory());

        init_phone_image_grid();
  }
  private void init_phone_image_grid() {

        String[] img = { MediaStore.Images.Thumbnails._ID };
        imagecursor = managedQuery(
  MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
  null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
        image_column_index = imagecursor
  .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        count = imagecursor.getCount();
        imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
        imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
        imagegrid.setOnItemClickListener(new OnItemClickListener(){
              public void onItemClick(AdapterView parent, View v,
   int position, long id) {
                    System.gc();
                    String[] proj = { MediaStore.Images.Media.DATA };
                    actualimagecursor = managedQuery(
   MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
   null, null, null);
                    actual_image_column_index = actualimagecursor
   .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    actualimagecursor.moveToPosition(position);
                    String i = actualimagecursor.getString(actual_image_column_index);
                    System.gc();
                   Intent intent = new Intent(getApplicationContext(),ViewImage.class);

                   intent.putExtra("filename", i);
                   startActivity(intent);
                                                            }
        });
       }


  public class ImageAdapter extends BaseAdapter {


      private Context mContext;
        public ImageAdapter(Context c) {
              mContext = c;
        }
        public int getCount() {
              return count;
        }
        public Object getItem(int position) {
              return position;
        }
        public long getItemId(int position) {
              return position;
        }
        public View getView(int position,View convertView,ViewGroup parent) {
              System.gc();
              ImageView i = new ImageView(mContext.getApplicationContext());
              if (convertView == null) {
                    imagecursor.moveToPosition(position);
                    int id = imagecursor.getInt(image_column_index);
                    i.setImageURI(Uri.withAppendedPath(
            MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));
                    i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    i.setLayoutParams(new GridView.LayoutParams(110,110));
              }
              else {
                    i = (ImageView) convertView;
              }
              return i;
          }
        }
         }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-11-11 14:31:39

您忘记了权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

You forgot a permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
安静 2024-11-11 14:31:39

尝试在 SDCard 中创建文件夹:

File folder = new File(Environment.getExternalStorageDirectory() + "/FolderName");
success = folder.mkdir();

确保您给出了:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在运行应用程序时卸载 SDCard,并在运行应用程序后安装它。

Try this for creating Folder inside SDCard :

File folder = new File(Environment.getExternalStorageDirectory() + "/FolderName");
success = folder.mkdir();

Ensure that you gave :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Also Unmount your SDCard while you run your Apps and after Running your Apps Mount it..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文