1

我看到在我的设备(Htc One S)中,图像存储在此路径上:storage/sdcard0/DCIM/100MEDIA/name_of_file.jpg

但我确信在其他设备中路径可能会有所不同。好吧,在我的应用程序中,我创建了一个服务器套接字,我可以成功地将图像发送到客户端。正如您在下面的代码中看到的,我在这种模式下创建路径Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_DCIM+"/100MEDIA","IMAG0026.jpg" `

这对我的设备有好处,但对另一个设备可能是错误的。所以现在我有一个问题......我如何在设备内或他的 SD 卡中搜索文件的 ID 或名称以发送它?这是我发送图像的代码(它工作正常)。

public class ServerThread extends AndroidApp2 implements Runnable { //dichiaro la classe ServerThread che implementa Runnable

    public void run() {
        try{
            if ( SERVERIP != null){ 
                handler.post(new Runnable(){
                    @Override
                    public void run(){
                        serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);                           
                    }
                }); 
                serverSocket = new ServerSocket(SERVERPORT);
                while (true){
                        Socket client = serverSocket.accept();
                        handler.post(new Runnable(){
                            @Override
                            public void run(){
                                serverStatus.setText("Connected");
                            }
                        });
                        try{
                            handler.post(new Runnable(){
                            @Override
                            public void run(){
                                AlertDialog.Builder connection_alert3 = new AlertDialog.Builder(AndroidApp2.this);
                                connection_alert3.setTitle("Connection Incoming");
                                connection_alert3.setMessage("Do you want accept the incoming connection?");
                                connection_alert3.setCancelable(false);
                                connection_alert3.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialog, int id) {
                                        alertValue = true;
                                    }
                                });
                                connection_alert3.setNegativeButton("No", new DialogInterface.OnClickListener(){
                                    public void onClick(DialogInterface dialog, int id){
                                        alertValue = false;
                                    }
                                });
                                AlertDialog alert = connection_alert3.create();
                                alert.show(); 
                            }
                            });
                            if (alertValue == true){
                            File xmlFile = new File(Environment.getExternalStorageDirectory(),"xmlPhotos.xml");
                            int size = (int) xmlFile.length();
                            byte[] bytes = new byte[size];
                            try {
                                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(xmlFile));
                                buf.read(bytes, 0, bytes.length);
                                buf.close();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            client.getOutputStream().write(bytes, 0, size); 
                            String[] projecton = new String[]{
                                    MediaStore.Images.Media.DATA,
                                };
                            Uri image = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                            Cursor cursor = managedQuery(image, projecton,"", null, "");
                            final ArrayList<String> imagesPath = new ArrayList<String>();
                            if(cursor.moveToFirst()){
                                int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                              do {
                                imagesPath.add(cursor.getString(dataColumn));
                              } while (cursor.moveToNext());
                            }

                            File photoFile = new File(Environment.getExternalStorageDirectory()+"/"+Environment.DIRECTORY_DCIM+"/100MEDIA","IMAG0026.jpg");
                            int size2 = (int) photoFile.length();
                            System.out.print("int2: "+size2);
                            byte[] bytes2 = new byte[size2];
                            try {
                                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(photoFile)); 
                                buf.read(bytes2, 0, bytes2.length);
                                buf.close();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            client.getOutputStream().write(bytes2, 0, size2);

                            serverStatus.setText("Finish the transfer");
                            client.close();
                            }
                        } catch (Exception e){
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones."); 
                                }
                            });
                            e.printStackTrace();
                        }
                    }
            } else{
                handler.post(new Runnable(){
                    @Override
                    public void run(){
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable(){
                @Override
                public void run(){
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
}

谢谢

4

1 回答 1

1

您可以通过以下方式获取图库中的图像列表:

String[] projection = new String[]{
        MediaStore.Images.Media.DATA,
};

Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = managedQuery(images,
        projection,
        "",
        null,
        ""
);

final ArrayList<String> imagesPath = new ArrayList<String>();
if (cur.moveToFirst()) {

    int dataColumn = cur.getColumnIndex(
            MediaStore.Images.Media.DATA);
    do {
        imagesPath.add(cur.getString(dataColumn));
    } while (cur.moveToNext());
}
于 2013-06-26T08:13:50.667 回答